Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Date as long value: how does TimeZone relate?

Could someone please explain in detail the following:

I have a long value which represents a date.

  1. What will be the timezone associated with the long value?

  2. How to convert the long value to a date with proper time zone?

  3. Is there is way to identify the timezone associated with the long date value?

like image 472
Kathir Avatar asked Oct 08 '12 03:10

Kathir


People also ask

How to get the time zone of a date in Java?

The Date class (which represents a specific instant in time) doesn't contain any time zone information. First, let's get the current UTC date and a TimeZone object: In Java 7, we need to use the Calendar class to represent a date with a time zone.

How to calculate day/date/time in a particular time zone?

The basic steps in the calculation of day/date/time in a particular time zone: Obtain a new Date object. A date format object is created in Simple Date Format. The required format is assigned to the date format object – For Eg: hh:mm:ss.SSS. Set the required time zone to the date format object.

What is date-time in Java?

Using Java 8 Java 8 introduced a new Date-Time API for working with dates and times which was largely based off of the Joda-Time library. The Instant class from Java Date Time API models a single instantaneous point on the timeline in UTC. This represents the count of nanoseconds since the epoch of the first moment of 1970 UTC.

How to pick the day/date/time value in Java timestamp format?

Pick the day/date/time value from the date format object by passing the date object created in the first step. The following table provides the various date patterns and their significance in Java Timestamp Format:


2 Answers

The long is milliseconds since Jan 1970, GMT. So, to that respect, it is GMT.

like image 69
user949300 Avatar answered Sep 28 '22 12:09

user949300


The long value which represents the java.util.Date is the number of milliseconds elapsed from epoch. (Jan 1, 1970)

/**
 * Allocates a <code>Date</code> object and initializes it to 
 * represent the specified number of milliseconds since the 
 * standard base time known as "the epoch", namely January 1, 
 * 1970, 00:00:00 GMT. 
 *
 * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.
 * @see     java.lang.System#currentTimeMillis()
 */
public Date(long date) {
    fastTime = date;
}
  • What will be the timezone associated with the long value?
    Can you attach a unit to a long value.? No.
    This is akin to saying given an int 2 what does it represent? . It could be 2 miles or 2 pounds.

  • How to convert the long value to a date with proper time zone?
    You can't because of above.

  • Is there is way to identify the timezone associated with the long date value?
    Nope.

like image 27
Ajay George Avatar answered Sep 28 '22 12:09

Ajay George