-1

This is my existing code snippet:

Calendar cal = Calendar.getInstance();  
cal.set(Calendar.HOUR_OF_DAY, 0);  
cal.set(Calendar.MINUTE, 0);  
cal.set(Calendar.SECOND, 0);  
cal.set(Calendar.MILLISECOND, 0);  

I just want to assign those attributes at once, something like:

Calendar cal = Calendar.getInstance();  
cal.set(Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND, 0);   

I am not sure it is possible or not, because I am new to java. Please help me if there is other way around to achive this.

If possible, please give others solutions also. Objective is to set the given cal object to 00:00:00.000A.M.

Edit 1: I think this question was clear and understandable. Please consider upvoting as I couldn't ask anymore questions on stackoverflow.

surya
  • 719
  • 5
  • 13
  • Just use SimpleDateFormat. You can find solutions on you question here https://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java/428966#428966 – Aleksei Grabor Feb 18 '20 at 15:28
  • 2
    @Aleksei Please don't. Use DateTimeFormatter – OneCricketeer Feb 18 '20 at 15:38
  • I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use one or more class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), for example `ZonedDateTime`. – Ole V.V. Feb 18 '20 at 17:02
  • 1
    The period in the middle of @cricket_007’s comment is quite important. – Ole V.V. Feb 18 '20 at 21:43

3 Answers3

2

If possible, avoid the use of the legacy Calendar class. Do use the classes from the java.time package, e.g.:

LocalDateTime todayAtStartOfDay = LocalDate.now().atStartOfDay();

But if Calendar must be used, then consider using Calendar.Builder, e.g,:

Calendar cal = new Calendar.Builder()
            .setTimeOfDay(0, 0, 0, 0)  // hour, minute, second, millis
            .build();
Andrew S
  • 2,509
  • 1
  • 12
  • 14
2

The old Calendar class can be set by the current generation of java time classes:

// Current time classes:
LocalDate today = LocalDate.now();
// LocalDate today = LocalDate.of(2020, 2, 18);
LocalDateTime morning = today.atStartOfDay();

// Old classes:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(morning.toEpochSecond()));
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

There is an overloaded set method in the Calendar class:

void java.util.Calendar.set(int year, int month, int date, int hourOfDay, int minute, int second)

Unfortunately you have to set your milliseconds manually. And you also have to use date formatter. Here an example

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS aa");
Calendar cal = Calendar.getInstance();  
cal.set(0, 0, 0, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(sdf.format(cal.getTime()));

It returns 00:00:00.000 AM

Planck Constant
  • 1,406
  • 1
  • 17
  • 19
  • You are 100% right. But first thing is that we don't know what Java version he uses. I suppose it is <8 because of `Calendar` class. Second thing it is just a simple example. He can improve it in many ways. – Planck Constant Feb 18 '20 at 15:49
  • Java 8+ still contains calendar class, but it's end of life anyway – OneCricketeer Feb 18 '20 at 16:02