Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Date year set at 1970

Tags:

java

I need to parse a String that does not originally have the year set like "13 Aug 11:30"

but when I output the date it adds the year on, I tried set year but the year is totally wrong the output comes as "Thu Aug 13 11:30:00 GMT 3911"

Is there a way to set the year after the date is parsed?

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

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


fromDate.setYear(2011);
like image 437
M_K Avatar asked Jan 24 '12 23:01

M_K


1 Answers

Use Calendar to set the year:

Calendar c = Calendar.getInstance();
c.setTime(fromDate);
c.set(Calendar.YEAR, 2011);
fromDate = c.getTime();
like image 129
Bhesh Gurung Avatar answered Sep 28 '22 17:09

Bhesh Gurung