bitflip-at
bitflip-at

Reputation: 149

java.util.date get milliseconds for y-m-d only

Situation: There is an Object AuditLog, which contains the variable java.util.Date date. This Object is saved in a mySQL Database.

@Entity
public class AuditLog implements Persistable<Long> {
    ...
    @Temporal(TemporalType.DATE)
    private Date date;
    ...
}

I am writing some JUnit tests and need to verify that a saved Date equals the actual date. Where date is a local Copy of the value actually passed to the log Object before it got saved and then loaded again.

Assert.assertEquals(date, log.getDate());

Output:
expected:<Wed May 24 15:54:40 CEST 2017> but was:<2017-05-24>

So you can see that the date actually is the right one but only y-m-d I then tried this (below) to check if the milliseconds get altered.

Assert.assertEquals(date.getTime(), log.getDate().getTime());

Output:
expected:<1495634973799> but was:<1495576800000>

Now i think the best way would be to get the Milliseconds for year month day only.

Question: Can this be achieved relatively simple and should i do this? I think the Date gets altered because of a Database operation of some kind, so adapting the Test is OK right?

Upvotes: 0

Views: 1024

Answers (4)

chocksaway
chocksaway

Reputation: 900

You can get the "just the day, month, year by using the following code:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Answer {
    public static void main(String args[]) throws ParseException {
        // parse the date and time
        String input = "Wed May 24 15:54:40 CEST 2017";
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date date = parser.parse(input);

        // parse just the date
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        formatter.setTimeZone(TimeZone.getTimeZone("CEST"));

        String formattedDate = formatter.format(date);

        Date parsedDate = formatter.parse(formattedDate);

        System.out.println(parsedDate);

        // use https://currentmillis.com/ to check the milliseconds figures
        System.out.println("Wed May 24 15:54:40 CEST 2017 in milliseconds \t" + date.getTime());

        System.out.println("Wed May 24 00:00:00 CEST 2017 in milliseconds \t" + parsedDate.getTime());
     }
 }

The second SimpleDateFormat("yyyy-MM-dd"); parses on the year-month-day.

Use Date.getTime()); to get the milliseconds.

The output is:

Wed May 24 15:54:40 CEST 2017 in milliseconds   1495634080000
Wed May 24 00:00:00 CEST 2017 in milliseconds   1495584000000

1495584000000 = Wed May 24 2017 00:00:00 (using https://currentmillis.com/)

Upvotes: 1

Darshan Mehta
Darshan Mehta

Reputation: 30849

There are two ways to do this:

  • Using local date : You can convert util Date to LocalDate and do assertEquals on both the objects. LocalDate won't have time, e.g.:

    Date input = new Date(); LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(date);

  • Using Apache commons' DateUtils: You can use truncate method to set non date fields to zero, e.g.:

    Date input = new Date(); Date truncated = DateUtils.truncate(input, Calendar.DATE); System.out.println(truncated);

    Here's the maven dependency for Apache commons library.

Upvotes: 2

XtremeBaumer
XtremeBaumer

Reputation: 6435

Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 5 - 1);
cal.set(Calendar.DAY_OF_MONTH, 24);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date d = cal.getTime();
System.out.println(d.getTime());

this code creates a new java.util.Date with only year, month and day set. result of this example is 1495576800000 which is what you want.

A shorter way would be this:

Date d = new Date(0l);
d.setYear(117);
d.setMonth(4);
d.setDate(24);
d.setHours(0);

Upvotes: 1

Olavo Holanda
Olavo Holanda

Reputation: 363

You should format the two dates:

Date date = new Date();
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
dt.format(date);

Then compare each other.

Upvotes: 0

Related Questions