Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set datetime to end of day in java

Tags:

java

I want to set datetime of day as: startDate=2018/03/28 00:00:00 and endDate=2018/03/28 23:59:59

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    String str1=sdf1.format(cal.getTime());
    String str2=sdf2.format(cal.getTime());
    Date startDate = sdf1.parse(str1);
    Date endDate = sdf2.parse(str2);

My problem:program is working and output endDate=2018/03/28 00:00:00

Would you please point out any mistakes to me in code?

update: i used debug and it's working correct with

String str2=sdf2.format(cal.getTime());//2018-03-28 23:59:59

but when change string==>date is not correct with output 2018/03/28 00:00:00

like image 591
dsadffg Avatar asked Mar 28 '18 01:03

dsadffg


People also ask

How to get end time of the day in Java?

From a LocalDate Object of(localDate, LocalTime. MIDNIGHT); LocalTime offers the following static fields: MIDNIGHT (00:00), MIN (00:00), NOON (12:00), and MAX(23:59:59.999999999). Therefore, if we want to get the end of the day, we'd use the MAX value.

How do you get start time and end of day?

Overview. Theoretically, the start of any day is the time of midnight, '00:00' when this day starts. Similarly, the end of the day is the time '23:59:59.999999999' just before midnight.

What does date () getTime () do in Java?

The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object.

How do I get the latest date in Java?

Get Current Date & Time: java.time.Clock The Clock.systemUTC().instant() method returns the current date and time both.


1 Answers

If you want to initialize Date instances from a formatted string with both date and time then time codes should be added to the SimpleDateFormat pattern to parse strings in that format.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse("2018-03-28 00:00:00");
Date endDate   = sdf.parse("2018-03-28 23:59:59");

If you want to simply set the hour, minute, and second on the current date then use a Calendar instance and set fields on it accordingly.

 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)

 Date startDate = cal.getTime();
 cal.set(Calendar.HOUR_OF_DAY, 23);
 cal.set(Calendar.MINUTE, 59);
 cal.set(Calendar.SECOND, 59);
 cal.set(Calendar.MILLISECOND, 999)
 Date endDate = cal.getTime();

And next output the Date in a particular format:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(sdf.format(startDate));
System.out.println(sdf.format(endDate));

Output:

2018/03/28 00:00:00
2018/03/28 23:59:59

Dealing with time zones

If time zone is other than the local time zone then it's a good idea to be explicit with what timezone you're working with. Calendar and SimpleDateFormat instances must be consistent with what timezone you're dealing with or the date and/or times may be off.

TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(utc);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
sdf.setTimeZone(utc);
like image 196
CodeMonkey Avatar answered Oct 04 '22 09:10

CodeMonkey