-2

I have to convert UTC time into user local time zone. Currently, I have the two parameters one is time in long format and another is time zone name in string format like "(UTC-05:00) Eastern Time (US and Canada), (UTC-06:00) Central Time (US and Canada)" etc.

So now using these two parameters I have to get date time in string format. I am facing the issue while I am trying to convert the date into a string because the SimpleDateFormat.format(...) will convert the date using its default time zone.

Below are the code portion

public static void main(String[] args) 
{
long time = 1490112300000L;
System.out.println("UTC Time "+ convertLongToStringUTC(time));

String EST = "(UTC-05:00) Eastern Time (US and Canada)";
TimeZone  timeZone1 = TimeZone.getTimeZone(EST);
System.out.println("EST "+ convertTimeZone(time, timeZone1));

String CST = "(UTC-06:00) Central Time (US and Canada)";
TimeZone timeZone2 = TimeZone.getTimeZone(CST); 
System.out.println("CST "+ convertTimeZone(time, timeZone2));

String IST =  "IST";
TimeZone timeZone = TimeZone.getTimeZone(IST);      
System.out.println("IST "+ convertTimeZone(time, timeZone));    

}

public String convertTimeZone(long time, TimeZone timeZone)
{
Date date = new Date(time);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(timeZone);
return format.format(date);
}   

public String convertLongToStringUTC(long time)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String utcTime = sdf.format(new Date(time));
return utcTime;
}

Also let me know if we can achieve this using offset ?

3 Answers3

1

Use this constructor

SimpleDateFormat(String pattern, Locale locale)

Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale. Note: This constructor may not support all locales. For full coverage, use the factory methods in the DateFormat class.

Java Doc

Yang
  • 858
  • 6
  • 22
1

Using Java 8 you can do

OffsetDateTime dt = Instant.ofEpochMilli(System.currentTimeMillis())
                            .atOffset( ZoneOffset.of("-05:00"));
//In zone id you put the string of the offset you want
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
1

tl;dr

Instant.ofEpochMilli( 1_490_112_300_000L )
       .atOffset( ZoneOffset.of( "-05:00" ) )

Instant.ofEpochMilli( 1_490_112_300_000L )
       .atZone( ZoneId.of( "America/New_York" ) )

Details

The Answer by Dennis is close. I will provide further information.

Your Question is not exactly clear about the inputs. I will assume your long integer number represents a moment in UTC.

An offset-from-UTC is an number of hours and minutes and seconds before or after UTC. In java.time, we represent that with a ZoneOffset.

While ZoneId technically works (as seen in code by Dennis), that is misleading as a zone is much more than an offset. A zone is a region’s history of various offsets that were in effect at different periods of history. A zone also includes any planned future changes such as DST cutovers coming in the next months.

ZoneOffset offset = ZoneOffset.of( 5 , 30 ); // Five-and-a-half hours ahead of UTC.

ZoneOffset offset = ZoneOffset.of( "+05:30" );

Tip: Always include the padding zero on the hours. While not always required in various protocols such as ISO 8601, I have seen software systems burp when encountering single-digit hours like +5:00.

If you know the intended time zone for certain, use it. A zone is always better than a mere offset as it brings all that historical information of other offsets for the past, present, and future.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Asia/Kolkata" );

I am guessing your number is a number of milliseconds since the epoch of 1970-01-01T00:00:00Z.

Instant instant = Instant.ofEpochMilli( 1_490_112_300_000L );

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

You can adjust into a time zone.

ZonedDateTime zdt = instant.atZone( z );

These issues have been covered many times in Stack Overflow. Hence the down-votes you are collecting (I am guessing). Please search Stack Overflow thoroughly before posting.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks Basil for your inputs. Instant.ofEpochMilli( 1_490_112_300_000L ).atOffset( ZoneOffset.of( "-05:00" ) ) This code portion is not handling the DST time. I don't have parameter for time zone name such as America/Montreal. Do you have any idea on this ? – user3467573 Mar 29 '17 at 07:17
  • can you look above comments – user3467573 Mar 29 '17 at 07:38
  • @user3467573 I do not understand your comment. Are you saying you expected a DST cutover that took effect at that date-time? Of course there is no DST cutover as you did not specify a time zone. As I explained, DST is defined within a time zone. You specify only an offset-from-UTC. Many places use an offset of five hours behind UTC. There is no way to know from `-05:00` what time zone is in play, and no time zone means no DST. – Basil Bourque Mar 29 '17 at 07:47
  • I only have the information like timeZone = "UTC-06:00) Central Time (US and Canada)" and long time = 1490112300000L; So using this how can I get the correct date time in user local time, I havn't any information like region specific such as "Asia/Kolkata" – user3467573 Mar 29 '17 at 08:48
  • @user3467573 (A) Convince your data source to provide true zone names. (B) Map those strings to what you presume is their intended time zone. For example "Central Time (US and Canada)" might fit `America/Chicago`. – Basil Bourque Mar 29 '17 at 18:51