Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting today's date in java - I've tried the regular ways

Tags:

java

date

I need today's date - and zero anything else (" 05/06/08 00:00:00 ")

I've tried

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR, 0);        
Date date1 = calendar.getTime();                             
System.out.println(date1);

Run: (This is seriously messed up)

If the hour on the computer is < 12:00 at noon : Sun Mar 08 00:44:39 IST 2009

If the hour on the computer is > 12:00 at noon : Sun Mar 08 12:46:53 IST 2009

So I gave this up.

All the Date's setters are deprecated (except the epoch time) - so I don't want to use them either

The only thing I could think of is

Calendar calendar = Calendar.getInstance();     
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sDate = dateFormat.format(calendar.getTime());
Date today = dateFormat.parse(sDate);

But this is such a lame code I can't bring myself to write it.

Any other option?

Thanks!

like image 835
Yossale Avatar asked May 06 '09 13:05

Yossale


People also ask

How do I get today's date in Java?

The now() method of LocalDate class The now() method of the Localdate class returns the Date object representing the current time.

How do you check if the date is in dd mm yyyy format in Java?

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); formatter. setLenient(false); try { Date date= formatter. parse("02/03/2010"); } catch (ParseException e) { //If input date is in different format or invalid. }

How do you change date format to MM DD YYYY in Java?

String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat( pattern ) ; The String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “ MM-dd-yyyy ”.


3 Answers

I use this:

public static Date startOfDay(Date date) {
   Calendar dCal = Calendar.getInstance();
   dCal.setTime(date);
   dCal.set(Calendar.HOUR_OF_DAY, 0);
   dCal.set(Calendar.MINUTE, 0);
   dCal.set(Calendar.SECOND, 0);
   dCal.set(Calendar.MILLISECOND, 0);

   return dCal.getTime();
 }
like image 96
Damo Avatar answered Nov 15 '22 03:11

Damo


My standard advice for Java date/time questions: don't use java.util.{Calendar,Date}. Use Joda Time. That way you can represent a date as a date (with no associated time zone), instead of a date/time. Or you could use a DateMidnight if that's what you want to represent. (Be careful of combinations of time zone and date where there is no midnight though...)

What do you need to use the Date with? If you can get away with changing to use Joda throughout, that's great. Otherwise, you can use Joda to do what you want and then convert to milliseconds (and then to java.util.Date) when you really need to.

(Michael's solution when using Date/Calendar is fine if you really want to stick within a broken API... but I can't overstate how much better Joda is...)

like image 35
Jon Skeet Avatar answered Nov 15 '22 03:11

Jon Skeet


You should use HOUR_OF_DAY instead of HOUR and combine it with MINUTE and SECOND also.

import java.util.Calendar;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.SECOND;
import static java.util.Calendar.MILLISECOND;

public class Today { 
    public static void main( String [] args ) { 
        Calendar cal = Calendar.getInstance();
        cal.set( HOUR_OF_DAY, 0 );
        cal.set( MINUTE, 0 );
        cal.set( SECOND, 0 );
        cal.set( MILLISECOND, 0 );
        System.out.println( cal.getTime() );
    }
}

The results you are getting are due to HOUR is used to AM/PM while HOUR_OF_DAY is 24 hrs.

HOUR_OF_DAY:

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

HOUR:

Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.

like image 26
OscarRyz Avatar answered Nov 15 '22 02:11

OscarRyz