Java – the default date year is set to 1970

I need to parse a string with no year set

But when I output the date, it will increase the year. I tried to set the year, but the year is completely wrong. The output comes from "Thu Aug 13 11:30:00 GMT 3911"

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

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM HH:mm");

String dateStr = "13 Aug 11:30";
Date fromDate = (Date)formatter.parse(dateStr);


fromDate.setYear(2011);

Solution

Set year using calendar:

Calendar c = Calendar.getInstance();
c.setTime(fromDate);
c.set(Calendar.YEAR,2011);
fromDate = c.getTime();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>