0

I know this is a similar question to many out there but I am stuck as to what I am doing wrong in my code.

The server has a timestamp from America and I live in India. I want the fetched timestamp to be converted to my timezone standards. I use this following code block:

SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");    
Date date = formatter.parse(serverDate); //Server Date              

        SimpleDateFormat sdfIndia = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
        TimeZone tzInIndia = TimeZone.getDefault();
        sdfIndia.setTimeZone(tzInIndia);

        String sDateInIndia = sdfIndia.format(date); //Date Output in String Format

The output time is the same as the input time. Why is it coming out to be the same? Any help where I did wrong?

CodexRat
  • 35
  • 9
  • Maybe this will help: http://stackoverflow.com/a/1661389/598641 – Axxxon Aug 03 '16 at 14:54
  • @Axxxon Well my problem is regarding the time conversion, it is unaffected by the code. The input time is the same as output time. – CodexRat Aug 03 '16 at 15:00

1 Answers1

0

The code below will print the date time in US/Eastern and Asia/Dubai time zones.

    Date today = new Date();
    DateFormat formatter= new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
    formatter.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
    System.out.println("US/Eastern Time: " + formatter.format(today));

    formatter.setTimeZone(TimeZone.getTimeZone("Asia/Dubai"));
    System.out.println("Asia/Dubai Time: " + formatter.format(today.getTime()));

Output at the time of this post:

US/Eastern Time: 03-8-2016 11:18:05 AM

Asia/Dubai Time: 03-8-2016 07:18:05 PM

The timezone ids "US/Eastern" and "Asia/Dubai" are from

    String[] ids = TimeZone.getAvailableIDs();
    for (String id : ids) {
        System.out.println(TimeZone.getTimeZone(id).getID());
    }
s-hunter
  • 24,172
  • 16
  • 88
  • 130
  • Thanks for your quick reply mate. Well I am aware of my Timezone Id of my region, I used `TimeZone.getDefault();` - works the same! But the problem is I am using a static datetime format, eg, "03-8-2016 11:18:05" as an input which i know has a timezone from America. I want that string to be converted to my regional time. My code is just spitting out the same input. – CodexRat Aug 03 '16 at 15:44
  • Assume your timezone is "Asia/Dubai", you got the time string 03-8-2016 11:18:05 and you want 03-8-2016 07:18:05, correct? – s-hunter Aug 03 '16 at 15:48
  • Ya, thats correct. but the problem with the conversion you showed is that, its giving the morning time of US/Eastern and you are getting the evening time of Asia/Dubai. – CodexRat Aug 03 '16 at 15:55
  • When it's 11AM in Eastern US, it's 7PM in Dubai, what's the problem about that? – s-hunter Aug 03 '16 at 16:02
  • well it aint showing am/pm beside any of the time. anyway, I need the output the way u asked earlier, can u input the string and get me a work around? – CodexRat Aug 03 '16 at 16:05
  • updated the answer, added an "a" to "dd-M-yyyy hh:mm:ss" >>>>>>> "dd-M-yyyy hh:mm:ss a", and it will show the AM or PM – s-hunter Aug 03 '16 at 16:23