Reputation: 1517
I want to convert:
Tue Mar 28 12:00:00 IST 2016 time format
INTO
Tue Mar 28 00:00:00 IST 2016
How can I do it?
=================
Actually
In the database, my date is saved with the format of "Tue Mar 28 00:00:00 IST 2016", I want to fetch this record using this date.but my following code
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.clear(Calendar.MILLISECOND);
return calendar.getTime();
but this is giving me Tue Mar 28 12:00:00 IST 2016, whereas I want Mar 28 00:00:00 IST 2016, So that it would be same as Database one, and will fetch.
Upvotes: 1
Views: 1555
Reputation: 14348
You need Calendar.HOUR_OF_DAY
constant, which is used to operate in 24-hour clock, instead of Calendar.HOUR
:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0); // <-- here
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.clear(Calendar.MILLISECOND);
Upvotes: 2
Reputation: 890
I think the OP wants to format the 24H time to 12H.
Try something like this:
SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm a");
Upvotes: 0
Reputation: 30839
You can just add 12 hours to date
object, with calendar
, e.g.:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2016-03-28");
System.out.println(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 12);
System.out.println(calendar.getTime());
Upvotes: 1