-1

I am trying to convert a date string in format "yyyyMMddHHmmss" to a date string for another Time Zone "America/Sao_Paulo" taking DST into consideration for America/Sao_Paulo ( which start on 16-oct-2016 ).

I am using TimeZone class as

TimeZone represents a time zone offset, and also figures out daylight savings.

import java.util.TimeZone;
import java.text.*;
import java.util.Date;;

public class TimeZoneConversion {

    public static void main(String[] args) 
    {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");     
            sdf.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
            try

            {   
                String inputdate = "20161016052355";
                Date t = sdf.parse(inputdate);  
                System.out.println(t);
            }
            catch(ParseException e)
            {

            }    
    }
}

Output for the above is shown in IST

Sun Oct 16 12:53:55 IST 2016

How can i convert the the time string with input "yyyyMMddHHmmss" to a time string in "America/Sao_Paulo" Time Zone with DST in output ?

shubham deodia
  • 169
  • 1
  • 4
  • 16
  • I tried, But it's not calculating the DST then. – shubham deodia Apr 14 '16 at 07:44
  • 1
    Your question is not clear. Can you very clearly divide your question into two *separate* halves, (1) **input** you are given, and (2) **output** you desire? – Basil Bourque Apr 14 '16 at 07:53
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 18 '17 at 02:25

4 Answers4

1

You are simply printing it in a timezone where it is not daylight savings time on that date - printing a Date prints it in the JVM's default timezone.

For instance, replacing System.out.println(t) with:

SimpleDateFormat out = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss zzzz");
out.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
System.out.println(out.format(t));

prints:

2016/10/16 05:23:55 Brasilia Summer Time

Ideone demo

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Then how can i convert it to DST time as "2016/10/16" is DST in Brazil and it is still printing the same time. – shubham deodia Apr 14 '16 at 08:00
  • I think that you might be confused by what DST means. That one time zone is DST doesn't mean that you need to describe the time in DST when converting to another time zone. – Andy Turner Apr 14 '16 at 08:12
  • But, that is my requirement. I need the Time to be converted into a DST time. – shubham deodia Apr 14 '16 at 08:21
  • 1
    Then you need to be converting it into a time zone which *is* in DST at that instant. It is *literally* meaningless to do it otherwise. – Andy Turner Apr 14 '16 at 08:23
1

java.time

You are using troublesome old date-time classes, now supplanted by the java.time classes.

Your input lacks any offset-from-UTC or time zone info. So we parse as a LocalDateTime, meaning any locality.

String input = "20161016052355";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f );

ldt.toString(): 2016-10-16T05:23:55

A LocalDateTime is not a moment, not a point on the timeline. To give it real meaning, you must provide the intended time zone (or offset).

ZoneId z = ZoneId.of( "America/Sao_Paulo" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

zdt.toString(): 2016-10-16T05:23:55-02:00[America/Sao_Paulo]

To see the same moment as UTC, extract an Instant.

Instant instant = zdt.toInstant() ;

instant.toString(): 2016-10-16T07:23:55Z

See this code run live at IdeOne.com.


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.

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

Please check with this...

please replace your specific time_zone

string dts = "May 16, 2010 7:20:12 AM CDT"; DateTime dt = DateTime.ParseExact(dts.Replace("CDT", "-05:00"), "yyyyMMddHHmmss", null);

  • My String is specifically in "yyyyMMddHHmmss" format, it's the requirement and i am only given time zone in this format "America/Sao_Paulo". – shubham deodia Apr 14 '16 at 07:48
0

This worked for me

try
{
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss");
f.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date time = f.parse("20161015113634");  


DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
formatter.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));

System.out.println(formatter.format(time)); 
}
catch (ParseException e){
    
} 

Input : 20161015113634 (Time which is not in DST)

Output: 20161015030634 (Converted time for SaoPaulo)

Input : 20161016113634 (Time in DST)

Output: 20161016040634 (Converted time for SaoPaulo with DST Offset)

Community
  • 1
  • 1
shubham deodia
  • 169
  • 1
  • 4
  • 16