0

I have Date instance representing elapsed day. Try to convert it to number of days and then backward gives different result.

public static long date4Serialization(Date date) {
    return TimeUnit.DAYS.convert(date.getTime(), TimeUnit.MILLISECONDS);
}

public static Date deserializeDate(long value) {
    return new Date(TimeUnit.MILLISECONDS.convert(value, TimeUnit.DAYS));
}

 public static void main(String []args){
    try {
        Date date = new SimpleDateFormat("dd.MM.yyyy").parse("05.05.2014");
        System.out.println(date);
        //Mon May 05 00:00:00 EDT 2014
        System.out.println(deserializeDate(date4Serialization(date)));
        //Sun May 04 20:00:00 EDT 2014T
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
 }

Any thoughts..

  • 1
    number of days elapsed as in days over in that year or since the epoch ? Can you please clarify that ? Also mention the Java version that you're using :) – Arkantos May 01 '15 at 06:50
  • since January 1, 1970, 00:00:00, tested on jdk versions 1.7.0_80, 1.8.0 – user2171283 May 01 '15 at 06:53
  • Why don't you use `new Date(value);` in your deserializeDate() ? – Arkantos May 01 '15 at 06:58
  • 1
    your question is not well written. It seems to me that you are asking why do you get this difference with your code, so i tried to reply to this question. (but you didn't wrote what difference you found (1 day,1 week or something else) – Gaucho May 01 '15 at 07:16

3 Answers3

2

As per the documentation, Date.getTime() returns:

the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

Note that it states 'GMT', while you create (or parse in this case) the date in your current time zone. This gives basically a partial number of days, rounded into a long. You yourself seem to suggest that you expect it in the 'EDT' timezone.

If it is only for serializing and deserializing purpose, why not just serializing using the number of milliseconds instead of the number of days. That would solve your problem right away?

[UPDATE]

Also I want to note that TimeUnit is part of the java.util.concurrent package. The scope of classes in this package is not to do date/time conversions, but to support concurrent programming.

If you really want to calculate using dates, then I would like to trigger your curiosity with this piece of Java 8 Code:

 public static void main(String []args){
    LocalDate zero = LocalDate.of(1970, Month.JANUARY, 1);
    LocalDate ld = LocalDate.now();
    System.out.println(ld);
    long days = zero.until(ld,ChronoUnit.DAYS);
    System.out.println("days: "+days);
    LocalDate ld2 = zero.plusDays(days);
    System.out.println(ld2);
 }

Key is the use of LocalDate, which gives you the presentation of a date without Time or Timezone.

Others might suggest the use of Joda-Time.

Community
  • 1
  • 1
YoYo
  • 9,157
  • 8
  • 57
  • 74
1

Looks like you're just trying to serialize a Date to long (milliseconds), and then deserialize back from long to Date.

So, that should be very simple. Just use:

public static long date4Serialization(Date date) {
    return date.getTime();
}

public static Date deserializeDate(long value) {
    return new Date(value);
}
yngwietiger
  • 1,044
  • 1
  • 11
  • 15
  • Offcource it works. But way does convertation to number of days and bacward not work? I want to serialize only significant digits of elapsed day, not the whole long.. – user2171283 May 01 '15 at 07:12
  • this answer doesn't reply to the question neither is an explanation of the fact – Gaucho May 01 '15 at 07:19
  • Well, the question isn't written very well. I answered as I understood it, which is why I said "Looks like...". – yngwietiger May 01 '15 at 07:20
0

your code may result in a error of 1 day due to the fact that the timeunit CONVERT function rounds to the lower value. read here the convert command: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#convert(long,%20java.util.concurrent.TimeUnit)

if the answer is not correct, detail on your question the difference that you found (random, many days or something else..)

Gaucho
  • 1,328
  • 1
  • 17
  • 32