开发者

How can I utilize SimpleDateFormat with Calendar?

开发者 https://www.devze.com 2023-02-23 07:38 出处:网络
I\'ve got GregorianCalendar instances and need to use 开发者_JAVA技巧SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to get needed out

I've got GregorianCalendar instances and need to use 开发者_JAVA技巧SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to get needed output. 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

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() returns a Date which can be used with SimpleDateFormat.


Simply call calendar.getTime() and pass the resulting Date object to the format method.


java.time

I recommend that you use java.time, the modern Java date and time API, for your date and time work. So not GregorianCalendar. Since a GregorianCalendar was holding all of date, time of day and time zone, the general modern substitute for it is 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:

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. Now formatting a ZonedDateTime is straightforward (and simpler than it was with a 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

In case you only need dates and no time of day or time zone, you need two changes:

  1. Use LocalDate instead of ZonedDateTime.
  2. Use DateTimeFormatter.ofLocalizedDate() instead of .ofLocalizedDateTime().

What if I really got a GregorianCalendar?

If you got a GregorianCalendar from a legacy API not yet upgraded to java.time, convert to 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号