Set java date year

I am trying to set the year java.util.Date .

It will mark the time that I need to analyze does not include the year, so I did this:

 private static final SimpleDateFormat logTimeStampFormat = new SimpleDateFormat("MMM dd HH:mm:ss.SSS"); boolean isAfterRefDate (String line, Date refDate) { try { Date logTimeStamp = logTimeStampFormat.parse(line); logTimeStamp.setYear(2012); // But this is deprecated! return logTimeStamp.after(refDate); } catch (ParseException e) { // Handle exception } } 

To avoid using an obsolete method, I like this:

 private static final SimpleDateFormat logTimeStampFormat = new SimpleDateFormat("MMM dd HH:mm:ss.SSS"); private static Calendar cal = Calendar.getInstance(); boolean isAfterRefDate (String line, Date refDate) { try { Date logTimeStamp = logTimeStampFormat.parse(line); cal.setTime(logTimeStamp); cal.set(Calendar.YEAR, 2012); logTimeStamp = cal.getTime(); return logTimeStamp.after(refDate); } catch (ParseException e) { // Handle exception } } 

I just don't think this is the best way to solve this problem. I must first properly configure the calendar object, and then return the date object from it, and earlier I could just change the date object directly.

Can anyone suggest a better approach?

+4
source share
2 answers

Can anyone suggest a better approach.

Of course - try to avoid using Date and Calendar in the first place. Use Joda Time , which is much better.

Setting the year to Date is an inherent ambiguous operation - what time zone should this year be? What would you expect if you set the year 2013 to the previous date on February 29, 2012?

Using Joda Time will make your code more understandable in terms of what kind of data you really expect. You can always convert to / from Date and Calendar at the borders of the API if you really need to.

+5
source

java.time

Can anyone suggest a better approach?

Yes, use the java.time classes.

Jon Skeet's answer was correct, but is now deprecated since the Joda-Time team advises switching to java.time classes.

MonthDay and LocalTime

We can parse this input string as two separate objects: MonthDay and a LocalTime . The first, obviously, is the month and day of the month, but without any year, so this is not a complete date. The second is the time of day, but without any date and without any time zone.

 String input = "Sep 21 12:34:56.123"; DateTimeFormatter f = DateTimeFormatter.ofPattern ( "MMM dd HH:mm:ss.SSS" ); MonthDay md = MonthDay.parse ( input , f ); LocalTime lt = LocalTime.parse ( input , f ); System.out.println ( "md: " + md + " | lt: " + lt ); 

md: --09-21 | lt: 12: 34: 56.123

LocalDateTime

We can add in a year, mix completely and get LocalDateTime .

 LocalDateTime ldt = LocalDateTime.of( ld , lt ); 

ZonedDateTime

This is not the present moment, not a point on the timeline. Without a UTC-offset context or time zone, a LocalDateTime does not make sense.

Apply ZoneId to get ZonedDateTime . Now we have a significant moment, a point on the timeline.

 ZoneId z = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ldt.atZone( z ); 
+1
source

All Articles