basickarl
basickarl

Reputation: 40444

Java Date format weird

I have an issue where I would like to get the start of the day, however it seems to be setting it to 12:00 via automatic.

SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");

today = dfFull.format(todayDate);
System.out.println(today);

Why is this spitting out:

2014-06-06 12:00:00

The 12:00:00 is the issue

Upvotes: 0

Views: 191

Answers (2)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

You should use HH for hours in this case.

SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");

today = dfFull.format(todayDate);
System.out.println(today);

Now you will get the out put as

2014-06-06 00:00:00

And again if you use hh that mean you are using 12 hour format while HH means 24 hour format

So

Upvotes: 1

Rahul
Rahul

Reputation: 45040

That is because hh represents the hour in 12 hour format. You need to use HH instead.

SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Have a look at the docs for more info.

Also, on a side note, there is a typo in your code.

SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date todayDate = dfFull.parse("2014-06-06 00:00:00"); // todayDate must be of type Date and not String
String today = dfFull.format(todayDate); // today should be of type String
System.out.println(today);

Upvotes: 11

Related Questions