-3

I want to take timezone from provided date and time as a string and want to convert it into local timezone datetime. My Simple date format is

"EEE yyyy-MM-dd 'at' hh:mm:ss aa zzz"

Accroding to this the date from client is coming as

Thu 2018-08-02 at 07:10:19 am PST

Now I want to take PST as timezone and convert date and time in local timezone (for eg. IST)

I am using JAVA 8.

Pratty
  • 85
  • 2
  • 13
  • You should try [this](https://stackoverflow.com/questions/6567923/timezone-conversion) – user12346352 Aug 06 '18 at 14:20
  • @user12346352 Not exactly a duplicate. That Question is more general, where this Question starts with a `java.util.Date` object in hand (apparently, not sure as Question is poorly composed). – Basil Bourque Aug 06 '18 at 21:33
  • 1
    You basically cannot. PST is ambiguous, it could be Phillipines Standard Time, Pitcairn Standard Time or Pacific Standard Time (don’t know if there are more possibilities). – Ole V.V. Aug 08 '18 at 08:39

2 Answers2

1

tl;dr

myJavaUtilDate                     // Some `java.util.Date` object. This troublesome class is obsolete.
.toInstant()                       // Convert from terrible legacy class to modern *java.time* class. Both represent a moment in UTC, always UTC.
.atZone(                           // Adjust from `Instant` in UTC to a `ZonedDateTime` in the wall-clock time used by the people of a particular region (a time zone).
    ZoneId.of( "Asia/Kolkata" )    // Specify time zone with `Continent/Region` name, never 3-4 letter pseudo-zone.
)                                  // Returns a `ZonedDateTime` object.
.toString()                        // Generate a String in standard ISO 8601 format extended to append name of zone.

java.time

So you have a java.util.Date object in hand? Convert from that terrible old outmoded class to its modern replacement, java.time.Instant.

Instant instant = myJavaUtilDate.toInstant() ;

Apply a time zone (ZoneId) to get a ZonedDateTime.

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" ) ;  
ZonedDateTime zdt = instant.atZone( z ) ;

To generate a string is standard ISO 8601 format wisely extended to append the name of the time zone in square brackets, call toString.

String output = zdt.toString() ;  // Standard ISO 8601 format, extended by appending name of zone.

For other formats, search Stack Overflow for DateTimeFormatter class. This has been discussed many many times already.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can Format your date like this:

Date date = new Date();

DateFormat formatter = new SimpleDateFormat("dd MM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(formatter.format(date));
user12346352
  • 176
  • 2
  • 20
  • 2
    Please do not suggest to a Java 8 user to use the `Date` or the `Calendar` classes. They are very outdated, and shouldn't be used in modern code. – RealSkeptic Aug 06 '18 at 14:26
  • The date was only a example but the `SimpleDateFormat` should not be outdated as far as I know – user12346352 Aug 06 '18 at 14:28
  • 1
    It is, because it works only with `Date`. Nowadays one should work with classes from the `java.time` and `java.time.format` packages. – RealSkeptic Aug 06 '18 at 14:33
  • I have also used `Date` and `SimpleDateFormat` in Java 9 and i have never gotten any Problem with it... Which Problem can apear if I still use this functions instead of the `Java.time.format` package – user12346352 Aug 06 '18 at 14:37
  • I have wriiten code 'DateFormat formatter = new SimpleDateFormat("EEE yyyy-MM-dd 'at' hh:mm:ss aa zzz"); Date date = formatter.parse("Thu 2018-08-06 at 08:10:19 pm IST"); System.out.println(date); formatter.setTimeZone(TimeZone.getDefault()); System.out.println(formatter.format(date));' And output is when my default timezone is EDT: Mon Aug 06 14:10:19 EDT 2018 Mon 2018-08-06 at 02:10:19 PM EDT which is not correct I guess. Can you tell me why? Am I missing something or doing wrong – Pratty Aug 06 '18 at 14:47
  • @Pratty What I see the second part of your date is correct, why does he print the first part too where does he get the first one? From your Code I can't understand that but I think `Mon 2018-08-06 at 02:10:19 PM EDT` is correct right – user12346352 Aug 06 '18 at 14:53
  • @user12346352 if we convert 08:10:19 IST to EDT it should be in between 10 to 11 AM not 2:00 right? – Pratty Aug 06 '18 at 16:00
  • Whether a given class is outdated or not isn’t a hard fact. Those who know certainly have considered `SimpleDateFormat` outdated for a long time now. I agree that suggesting it is poor advice. – Ole V.V. Aug 08 '18 at 08:38