-4

I Need to change the time value as 0. My Input is in the format :2016-07-30T06:00:00:000Z Expected Result:2016-07-30 00:00:00 I tried some code, but it will not work

final Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(timezone);
        calendar.setTime(date);
        calendar.set(Calendar.0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

kindly suggest some solution. thanks in advance.

  • 5
    `Calendar.0` seems wrong. – MikeCAT Jun 09 '16 at 07:26
  • 2
    define not working – Scary Wombat Jun 09 '16 at 07:26
  • 5
    Possible duplicate of [Set time to 00:00:00](http://stackoverflow.com/questions/17821601/set-time-to-000000) – eol Jun 09 '16 at 07:55
  • FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 26 '19 at 22:09

3 Answers3

1

If you are on Java 8 you can use the new date and time library:

date.toInstant().truncatedTo(ChronoUnit.DAYS)

which will give you the right instant. You can easily convert it back to a Date instance by using the Date.from(Instant) factory method.

Per Huss
  • 4,755
  • 12
  • 29
  • The code in this Answer goes to the first moment of the day **in UTC only**. If you want the start of the day in another time zone, use `date.toInstant().atZone( yourZoneIdHere ).toLocalDate().atStartOfDay( yourZoneIdHere )` – Basil Bourque May 26 '19 at 22:08
1

Your line

calendar.set(Calendar.0); 

is the problem.

People often mistakenly do

calendar.set(Calendar.HOUR, 0); 

which fails, while

 calendar.set(Calendar.HOUR_OF_DAY, 0); 

is the correct way

e.g. this code works

Date date = new Date();
final Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
Date zeroedDate = calendar.getTime();  
System.out.println(zeroedDate);

when run gives me

Thu Jun 09 00:00:00 NZST 2016

Ruan Malan
  • 41
  • 4
  • Good point, that's an easy trap to fall into! Beware of the time zone though, you will get zero hours in your local time zone... – Per Huss Jun 09 '16 at 08:05
0

Here is an example of how to use the Calendar

  String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
    "Sep", "Oct", "Nov", "Dec" };

Calendar calendar = Calendar.getInstance();

System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));

System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));

calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.SECOND, 22);

System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
 }
}

So I would for you is get rid of this

 calendar.setTimeZone(timezone);
 calendar.setTime(date);
 calendar.set(Calendar.0);

and change some of your code to this

calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

This should work I haven't tested it.

For more information go here http://www.java2s.com/Code/JavaAPI/java.util/Calendarsetintfieldintvalue.htm

MNM
  • 2,673
  • 6
  • 38
  • 73