I want to convert from UTC timezone (ex.2016-05-19T06:10:00) which is String to CEST timezone(2016-05-19T08:10:00) to String in java.
            Asked
            
        
        
            Active
            
        
            Viewed 7,360 times
        
    -1
            
            
        - 
                    Also see http://stackoverflow.com/questions/12487125/java-how-do-you-convert-a-utc-timestamp-to-local-time – Unknown May 19 '16 at 12:38
- 
                    1There are lots of similar questions on stack overflow. Please search before ask. Thanks. – Sanjeev May 19 '16 at 12:40
- 
                    I have gone through them but was not useful . – Koneru KiranKumarReddy May 19 '16 at 12:56
- 
                    1What do you mean? These questions shows how to convert between two timezones, just set zones you want. – Pshemo May 19 '16 at 13:00
1 Answers
5
            
            
        A java.time solution:
public static void main(String[] args) {
    String orgDate = "2016-05-19T06:10:00";
    String dstDate = LocalDateTime.parse(orgDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                                  .atZone(ZoneId.of("UTC"))
                                  .withZoneSameInstant(ZoneId.of("CET"))
                                     // CET is deprecated timezone, see list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
                                  .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
                                            // or use a custom formatter
    System.out.println(orgDate);
    System.out.println(dstDate); 
}
Result:
2016-05-19T06:10:00
2016-05-19T08:10
 
    
    
        Dariusz
        
- 21,561
- 9
- 74
- 114
- 
                    sorry I dont understand as LocalDateTime is not present in java.Util package, so it is showing errors. – Koneru KiranKumarReddy May 19 '16 at 12:59
- 
                    2
- 
                    1@KoneruKiranKumarReddy Never use the date-time classes found outside the `java.time` packages. Never use `java.util.Date`, `Calendar`, `SimpleDateFormat`, and such. They are terrible, bad design, poor implementation, built by people who did not understand the complexity of date-time handling. – Basil Bourque Jul 27 '19 at 01:14
- 
                    Good Answer, *except* for the part where you call `.toLocalDateTime()`. The `LocalDateTime` class purposely lacks any concept of time zone or offset-from-UTC. And so that class cannot represent a moment. In calling `toLocalDateTime`, you are discarding valuable information (the time zone), rendering the resulting ambiguous. While you do so here only as a shortcut to formatting, I suggest that is bad habit, abusing the nature and purpose of a class merely for the convenience of its `toString` method. Better to call `.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) ` – Basil Bourque Jul 27 '19 at 01:18
- 
                    1@BasilBourque I agree. I like the "philosophy" behind the lack of `toLocalDateTime` conversion. I edited my answer. – Dariusz Jul 28 '19 at 04:58
- 
                    Good. But I have another complaint: `CET` is not a [real time zone name](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). Such 2-4 character names are pseudo-zones. A real time zone has a name in `Continent/Region` format such `Europe/Paris`, `Europe/Berlin`, `Africa/Tunis`. – Basil Bourque Jul 28 '19 at 05:58
- 
                    @BasilBourque Agreed, corrected again. I left the old value on purpose, so that others can learn that there is such a thing like a deprecated time zone. As I did, just now. – Dariusz Jul 28 '19 at 07:04
