Yossale
Yossale

Reputation: 14361

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

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!

Upvotes: 14

Views: 36673

Answers (11)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78995

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

The modern Date-Time API has many types which truly represent a date or time or date-time in a specific timezone. You can choose from the following options as per your specific requirement:

  1. If you are looking for a type that represents a date without a timezone, you can use LocalDate.now. The good news is that its variant, LocalDate#now(ZoneId) returns the current date from the system clock in the specified time-zone.
  2. If you are looking for an object that represents a date without a timezone, and with time units set to zero, you can call LocalDate#atStartOfDay on the object obtained with Option#1.
  3. If you are looking for an Instant representing the Date-Time object obtained with Option#2, you can attach this object with ZoneId.of("Etc/UTC") using LocalDateTime#atZone to obtain a ZonedDateTime and convert the same into an Instant using ZonedDateTime#toInstant.

Demo:

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDate todayInSystemTz = LocalDate.now();
        System.out.println(todayInSystemTz);

        LocalDate todayInIndia = LocalDate.now(ZoneId.of("Asia/Kolkata"));
        System.out.println(todayInIndia);

        LocalDateTime todayInSystemTzWithZeroTimeUnits = todayInSystemTz.atStartOfDay();
        System.out.println(todayInSystemTzWithZeroTimeUnits);

        ZonedDateTime todayInUtcWithZeroTimeUnits = todayInSystemTzWithZeroTimeUnits.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(todayInUtcWithZeroTimeUnits);

        Instant instant = todayInUtcWithZeroTimeUnits.toInstant();
        System.out.println(instant);

        // Can I represent the obtained Instant in India?
        System.out.println(instant.atZone(ZoneId.of("Asia/Kolkata")));

        // Can I represent the obtained Instant in New York?
        System.out.println(instant.atZone(ZoneId.of("America/New_York")));
    }
}

Output:

2021-06-20
2021-06-20
2021-06-20T00:00
2021-06-20T00:00Z[Etc/UTC]
2021-06-20T00:00:00Z
2021-06-20T05:30+05:30[Asia/Kolkata]
2021-06-19T20:00-04:00[America/New_York]

ONLINE DEMO

The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

For any reason, if you need to convert this object of Instant to an object of java.util.Date**, you can do so as follows:

Date date = Date.from(instant);

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

** A java.util.Date object simply represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). Since it does not hold any timezone information, its toString function applies the JVM's timezone to return a String in the format, EEE MMM dd HH:mm:ss zzz yyyy, derived from this milliseconds value. To get the String representation of the java.util.Date object in a different format and timezone, you need to use SimpleDateFormat with the desired format and the applicable timezone e.g.

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);

sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String strDateNewYork = sdf.format(date);

sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String strDateUtc = sdf.format(date);

Upvotes: 2

zorman2000
zorman2000

Reputation: 19

I know this is a very old question, no longer active, but it came to be on the top when I searched Google.

While all advise is very good, I can't believe no one simply answered:

Date date = new Date(System.currentTimeMillis());
System.out.println(date);

Which returns effectively, today's date.

Upvotes: 1

stenix
stenix

Reputation: 3106

...or you can do it the hacker way:

long MS_PER_DAY = 86400000L;
Date dateTime=new Date();
long offset = TimeZone.getDefault().getOffset(dateTime.getTime());
Date date= new Date(((dateTime.getTime()+offset)/MS_PER_DAY)*MS_PER_DAY-offset);

Upvotes: 1

Clinton
Clinton

Reputation: 2837

As mentioned above you should use

Calendar.HOUR_OF_DAY

As opposed to

Calendar.HOUR

Also you need to clear out the other fields (Calendar.MINUTE, Calendar.SECOND, and Calendar.MILLISECOND) by setting them to zero.

Sorry there's no easy way here. A pain, and that's why they're working on a new API for Java 7 I believe based on Joda Time.

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199215

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.

Upvotes: 10

Charlie
Charlie

Reputation:

Another vote for JodaTime.

java.util.Date and Calendar are so bad they are broken. (And SimpleDateFormat is rubbish too!)

For what it's worth, Java 7 will include a new date time library based strongly around JodaTime.

Upvotes: 0

Steve K
Steve K

Reputation: 19586

See Apache's commons-lang DateUtils.truncate()

Upvotes: 4

Damo
Damo

Reputation: 11540

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();
 }

Upvotes: 26

Kurley
Kurley

Reputation: 152

Why the string manipulation?

Can you not just set the values you need on the Calendar object before converting to a Date using getTime()?

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499830

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...)

Upvotes: 18

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

The time component is not just hours (and Calendar.HOUR is, as you have noticed, AM/PM). You need to set all of the time fields to 0: HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND.

Upvotes: 9

Related Questions