简体   繁体   English

如何在日历中使用 SimpleDateFormat?

[英]How can I utilize SimpleDateFormat with Calendar?

I've got GregorianCalendar instances and need to use SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to get needed output.我有 GregorianCalendar 实例,需要使用 SimpleDateFormat (或者可能与日历一起使用但提供所需的 #fromat() 功能的东西)来获得所需的输出。 Please, suggest work arounds as good as permanent solutions.请提出与永久解决方案一样好的变通方法。

Try this: 试试这个:

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.setTimeZone(cal.getTimeZone());
System.out.println(dateFormat.format(cal.getTime()));

eQui's answer is missing a step eQui的答案错过了一步

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
#---- This uses the provided calendar for the output -----
dateFormat.setCalendar(cal); 
System.out.println(dateFormat.format(cal.getTime()));

Calendar.getTime()返回一个可以与SimpleDateFormat一起使用的Date。

只需调用calendar.getTime()并将生成的Date对象传递给format方法。

java.time时间

I recommend that you use java.time, the modern Java date and time API, for your date and time work.我建议您使用 java.time,现代 Java 日期和时间 API,来处理您的日期和时间工作。 So not GregorianCalendar .所以不是GregorianCalendar Since a GregorianCalendar was holding all of date, time of day and time zone, the general modern substitute for it is ZonedDateTime .由于GregorianCalendar所有日期、时间和时区,因此现代的通用替代品是ZonedDateTime

You didn't specify what needed output would be.您没有指定需要的输出是什么。 I am assuming that we want output for a human user.我假设我们想要为人类用户输出。 So use Java's built in localized format for the user's locale:因此,对于用户的语言环境,请使用 Java 的内置本地化格式:

private static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
                .withLocale(Locale.forLanguageTag("es"));

I specified Spanish language just as an example.我指定了西班牙语作为示例。 If you want to use the JVM's default locale, you may either specify Locale.getDefault(Locale.Category.FORMAT) or leave out the call to withLocale() completely.如果您想使用 JVM 的默认语言环境,您可以指定Locale.getDefault(Locale.Category.FORMAT)或完全withLocale()withLocale()的调用。 Now formatting a ZonedDateTime is straightforward (and simpler than it was with a GregorianCalendar ):现在格式化ZonedDateTime很简单(而且比使用GregorianCalendar更简单):

    ZonedDateTime zdt = ZonedDateTime.of(
            2011, 4, 11, 19, 11, 15, 0, ZoneId.of("Australia/Perth"));
    System.out.println(zdt.format(FORMATTER));

Output from this example:此示例的输出:

11 de abril de 2011, 19:11:15 AWST 2011 年 4 月 11 日,AWST 19:11:15

In case you only need dates and no time of day or time zone, you need two changes:如果您只需要日期而不需要一天中的时间或时区,则需要进行两项更改:

  1. Use LocalDate instead of ZonedDateTime .使用LocalDate而不是ZonedDateTime
  2. Use DateTimeFormatter.ofLocalizedDate() instead of .ofLocalizedDateTime() .使用DateTimeFormatter.ofLocalizedDate()而不是.ofLocalizedDateTime()

What if I really got a GregorianCalendar ?如果我真的得到了GregorianCalendar怎么办?

If you got a GregorianCalendar from a legacy API not yet upgraded to java.time, convert to ZonedDateTime :如果您从尚未升级到 java.time 的遗留 API 获得GregorianCalendar ,请转换为ZonedDateTime

    GregorianCalendar cal = new GregorianCalendar(
            TimeZone.getTimeZone(ZoneId.of("Australia/Perth")));
    cal.set(2011, Calendar.APRIL, 11, 19, 11, 15);
    ZonedDateTime zdt = cal.toZonedDateTime();

Then proceed as before.然后像以前一样继续。 Output will be the same.输出将是相同的。

Link关联

Oracle tutorial: Date Time explaining how to use java.time. Oracle 教程:解释如何使用 java.time 的日期时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM