Date Convert DateZone to java?

I was looking for the easiest way to convert a date from GMT to my local time. Of course, the availability of suitable DST dates is considered as standard as possible.

The most direct code I could find was:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String inpt = "2011-23-03 16:40:44"; Date inptdate = null; try { inptdate = sdf.parse(inpt); } catch (ParseException e) {e.printStackTrace();} Calendar tgmt = new GregorianCalendar(TimeZone.getTimeZone("GMT")); tgmt.setTime(inptdate); Calendar tmad = new GregorianCalendar(TimeZone.getTimeZone("Europe/Madrid")); tmad.setTime(inptdate); System.out.println("GMT:\t\t" + sdf.format(tgmt.getTime())); System.out.println("Europe/Madrid:\t" + sdf.format(tmad.getTime())); 

But I think I did not get the right concept, for which getTime will return.

+6
java timezone datetime
source share
5 answers

The catch here is that there is a time zone for the DateFormat class. Instead, try this example:

  SimpleDateFormat sdfgmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdfgmt.setTimeZone(TimeZone.getTimeZone("GMT")); SimpleDateFormat sdfmad = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdfmad.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); String inpt = "2011-23-03 16:40:44"; Date inptdate = null; try { inptdate = sdfgmt.parse(inpt); } catch (ParseException e) {e.printStackTrace();} System.out.println("GMT:\t\t" + sdfgmt.format(inptdate)); System.out.println("Europe/Madrid:\t" + sdfmad.format(inptdate)); 
+12
source share

You need to set TimeZone to SimpleDateFormat using DateFormat.setTimeZone() .

0
source share

For input, you can simply add the time zone to the line (note the "z" in the format):

 new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss z").parse ("2011-23-03 16:40:44 GMT"); 
0
source share

The easiest way is to use a decent time library, rather than the notorious java.util.Date and .Calendar classes. Instead, use Joda-Time or the java.time package found in Java 8.

Joda time

 String input = input.replace( " ", "T" ).concat( "Z" ) ; // proper ISO 8601 format for a date-time in UTC. DateTimeZone timeZone = DateTimeZone.forID( "Europe/Madrid" ); DateTime dateTime = new DateTime( input, timeZone ); String output = dateTime.toString(); 
0
source share

Here is the answer of 2017.

  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String inpt = "2011-03-23 16:40:44"; ZonedDateTime madridTime = LocalDateTime.parse(inpt, dtf) .atOffset(ZoneOffset.UTC) .atZoneSameInstant(ZoneId.of("Europe/Madrid")); System.out.println("GMT:\t\t" + inpt); System.out.println("Europe/Madrid:\t" + madridTime.format(dtf)); 

Please enjoy how more naturally this code expresses intent.

Code prints

 GMT: 2011-03-23 16:40:44 Europe/Madrid: 2011-03-23 17:40:44 

(with tabbed size 8 it aligns well, but StackOverflow seems to use tab size 4).

I swapped 23 and 03 in your input line, I suppose you intended to do this. By the way, I did not catch your mistake, it was LocalDateTime.parse() , throwing an exception because there is no 23rd month. Also in this regard, modern classes are more useful than obsolete ones.

Joda-Time? Vasily Burka's answer mentions and recommends both java.time , which I use, and Joda-Time. Although Joda-Time already significantly improves the obsolete classes used in the question ( SimpleDateFormat , Calendar , GregorianCalendar ), it is already in maintenance mode; no further development is expected. java.time extremely inspired by Joda-Time. For the new code, I see no reason why you should not prefer java.time .

0
source share

All Articles