Gagan
Gagan

Reputation: 129

Date formatting issue in java

When I parsing time in java, I passing "12:12" as string argument, then I am getting "Thu Jan 01 12:12:00 IST 1970" as a output.

I want current year like "Fri Mar 09 12:12:00 IST 2012" as a output.

String timestr = "12:12";
Date convertedDate = null;
try {
  SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
  convertedDate = dateFormat.parse(timestr);
} catch (ParseException e) {
  e.printStackTrace();
}
System.out.println(convertedDate);

Thanks!

Upvotes: 0

Views: 124

Answers (1)

npinti
npinti

Reputation: 52205

I think that the problem with this is that you are creating a date with null values and then just initialize the time value. I think you should use the Calendar class and get an instance of the Calendar and then set the time. Once that is done, you create a date object from the Calendar and parse it to your needs.

Upvotes: 6

Related Questions