I was trying to get month value. But I wanted to check which is better, java.util or java.time to retrieve month value. This is my code to check performance of Calender vs ZonedDateTime.
//import java.time.Instant;
//import java.time.ZonedDateTime;
//import java.util.Calendar;
Instant instant, instant2;
String diff;
instant = Instant.now();
int month2 = Calendar.getInstance().get(Calendar.MONTH) + 1;
instant2 = Instant.now();
diff = String.valueOf(instant2.getNano() - instant.getNano());
System.out.println("month value " + month2 + "at: " + diff);
instant = Instant.now();
int month1 = ZonedDateTime.now().getMonth().getValue();
instant2 = Instant.now();
diff = String.valueOf(instant2.getNano() - instant.getNano());
System.out.println("month value " + month1 + "at: " + diff);
I thought java.time was better than java.util. Hence I was expecting ZonedDateTime to perform better than Calendar. But here I found the inverse. My result was:
month value 6at: 0 //Calendar
month value 6at: 9000000 //ZonedDateTime
Any ideas why this is happening. And any suggestion on why I should thus use java.util.Calender instead of java.timeZonedDateTime.
P.S. I even reversed retrieving month2 after month1 as:
Instant instant, instant2;
String diff;
instant = Instant.now();
int month1 = ZonedDateTime.now().getMonth().getValue();
instant2 = Instant.now();
diff = String.valueOf(instant2.getNano() - instant.getNano());
System.out.println("month value " + month1 + "at: " + diff);
instant = Instant.now();
int month2 = Calendar.getInstance().get(Calendar.MONTH) + 1;
instant2 = Instant.now();
diff = String.valueOf(instant2.getNano() - instant.getNano());
System.out.println("month value " + month2 + "at: " + diff);
Still same:
month value 6at: 8000000 //ZonedDateTime
month value 6at: 0 //Calendar