I have a utility written in my application that takes in a timezone and returns me a date (java.util.Date) as follows -
// Inside MyDateHelper.java
public static java.util.Date getTimeForTimeZone(String timeZoneId)
{
final Date currentDate = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId)).getTime();
return currentDate;
}
I researched around the internet to find that Date does not have any timeZone information in it. Rather it is a wrapper around a long value starting from the epoch of 1970. So, my question is if I have to get the current Date-Time instant a transaction has occurred to store in a date field (of type java.util.Date), Is this utility of any use?
For all practical purposes are the two lines of code achieving the same?
transactionEntry.setTime(MyDateHelper.getTimeForTimeZone("UTC+7:00"));
OR
transactionEntry.setTime(new Date());
I also checked out the new Java 8 Time API, but for my purpose I think doing this is also an overkill and achieves nothing more new Date()
-
Date date4 = new Date();
LocalDateTime localDate = date4.toInstant().atZone(ZoneId.of("UTC+07:00")).toLocalDateTime();
final ZonedDateTime dateWithZone = localDate.atZone(ZoneId.of("UTC+07:00"));
Date dateOutput = Date.from(dateWithZone.toInstant());
transactionEntry.setTime(dateOutput);
The whole purpose is to capture the time a transaction occurred with the time zone, but I can only store the time as a java.util.Date. No matter in which timezone the transaction occurred, the Date will be able to capture the instant of time correctly, right? Can someone confirm that this understanding is correct?