0

I have a Calendar object manipulated according to my needs, but converting it to Instant is not giving me the correct result:

Calendar cal = Calendar.getInstance(); // creates calendar
cal.setTime(inputFiledate); // sets calendar time/date --> inputFiledate is 29-12-2015
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(inputFileHour)); -->inputFileHour is 5
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.HOUR_OF_DAY,3); // adds  hours that is now time is 29-12-2015 08:00:00
System.out.println("Date after manipulation "+cal.getTime()); -->Displays 
TimeZone tz = TimeZone.getTimeZone("UTC");

// set the time zone with the given time zone value 
// and print it
cal.setTimeZone(tz);
// Date date = cal.getTime(); // returns new date object, one hour in the future
Date d= cal.getTime();
System.out.println("DAte to Instant "+ d.toInstant()); 
System.out.println(" Calendar to Instant "+cal.toInstant());
System.out.println("Date after manipulation2 "+cal.getTime());

This is the output:

Date after manipulation Tue Dec 29 08:00:00 IST 2015
DAte to Instant 2015-12-29T02:30:00Z
Calendar to Instant 2015-12-29T02:30:00Z
Date after manipulation2 Tue Dec 29 08:00:00 IST 2015

In need to convert this Calendar object to instant but it is giving incorrect result 2015-12-29T02:30:00Z where as the output should be 2015-12-29T08:00:00Z Where m I going wrong?

Also tried with Zoned datetime, with Timezone, in vain.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
user2176576
  • 734
  • 3
  • 14
  • 39
  • What do you think `System.out.println("DAte to Instant "+ d.toInstant())` does? How do you think your `Instant` is converted to a `String`? – Boris the Spider Dec 30 '15 at 13:20
  • I do not see your problem. 08:00:00 in your timezone is 02:30:00 in UTC. So what's wrong? – Ctx Dec 30 '15 at 13:23
  • @Ctx I think the OP doesn't understand how the `toString` method of `Instant` works... – Boris the Spider Dec 30 '15 at 13:24
  • I have an Epoch date inputEpochDate that i need to check is lesser than myDate(29 december 8:00:00 one that Ia m calculating using manipulation) inputFileTime =1451372373 that is 29 december 6:59 Instant inputEpochDate = Instant.ofEpochSecond( Long.parseLong(inputFileTime ) ); gives an instant 2015-12-29T06:59:33Z where as my Instant returned is 2015-12-29T02:30:00Z and it should actually return 2015-12-29T08:00:00Z – user2176576 Dec 30 '15 at 13:34
  • Let me put it in a simpler way: I need to compare an Epoch time with a Calendar time – user2176576 Dec 31 '15 at 05:28

1 Answers1

1

Date doesn't have a time zone. From the time difference I assume you are using Indian Time (-5:30) It is just the time relative to epoch which is different in each time zone.

I suggest you do the calculation using ZonedDateTime instead of trying to convert from a Calendar.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    It's not so much `Date`, but `Instant`. And it's not so much its lack of a timezone, but the default timezone of the `toString` conversion - which in the case of `Instant` uses `UTC` rather than the system default timezone. – Boris the Spider Dec 30 '15 at 13:46
  • 1
    Thanks everyone! Figuresd Out ZonedDateTime is the only option out: this is how I managed to do the Comparison: LocalDate date = LocalDate.parse("29-12-2015", DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ENGLISH)); ZonedDateTime zdt = date.atStartOfDay(ZoneOffset.UTC); System.out.println(zdt); ZonedDateTime zdt1 = zdt.plusHours(5); ZonedDateTime zdt2 = zdt1.plusHours(3); System.out.println(zdt1); System.out.println(zdt2); System.out.println(zdt2.toInstant()); return zdt2.toInstant(); – user2176576 Dec 31 '15 at 06:08
  • 1
    did not really understand why the question was downvoted though, as it was different from those answered before, I had to compare an Epoch time to another Manipulated time – user2176576 Jan 01 '16 at 07:12
  • @user2176576 I rarely downvote and prefer to comment so there is a chance to learn something. – Peter Lawrey Jan 01 '16 at 08:02