Default Date Established in 1970

I need to parse a string that initially does not have a set year of "August 13 11:30"

but when I give out the date on which the year is added, I tried the given year, but the year is completely wrong, the output goes like "Thu Aug 13 11:30:00 GMT 3911"

Is there any way to set the year after parsing the date?

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM HH:mm"); String dateStr = "13 Aug 11:30"; Date fromDate = (Date)formatter.parse(dateStr); fromDate.setYear(2011); 
+6
source share
3 answers

Use Calendar to set the year:

 Calendar c = Calendar.getInstance(); c.setTime(fromDate); c.set(Calendar.YEAR, 2011); fromDate = c.getTime(); 
+11
source

Have you read the APIs for the setYear method?

Sets the year of this Date object for the specified value plus 1900

2011 + 1900 = 3911

+3
source

The documentation for java.util.Date clearly states that setYear() adds 1900 to the year passed. Just subtract 1900 from your argument.

+3
source

Source: https://habr.com/ru/post/906803/


All Articles