0

I am receiving following JSON Array in this format from back end

[
    {
        "time": "4:40pm",
        "country": "Australia"
    },
    {
        "time": "3:30pm",
        "country": "america"
    },
    {
        "time": "6:30am",
        "country": "mexico"
    }
]

I need to parse this json and convert each time to IST (Indian) time

I started this way , i am setting the timezone , but couldn't able to convert this time to IST

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Testing {
    public static void main(String[] args) throws ParseException {
        String time = "4:40pm";
        String country = "Australia";
        convertDate(time, country);
    }

    public static String convertDate(String time, String country)
            throws ParseException {
        SimpleDateFormat in = new SimpleDateFormat("hh:mm");

        if (country.equals("Australia")) {
            in.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
            in.parse(time).toString();

        }
        return "";
    }
}
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • Please see this http://stackoverflow.com/questions/6567923/timezone-conversion – Shankar Nov 02 '15 at 14:13
  • 1
    What time zone do you thing "america" means? There are lots of time zones in the US... also note that you're calling `toString()` but always just returning ""... and you're ignoring the return value of the method anyway. It's unclear what you *expect* this code to achieve... – Jon Skeet Nov 02 '15 at 14:14
  • @Preethi You can convert to UTC time and then convert to IST which will be two step and always work for every time zone conversion – Shankar Nov 02 '15 at 14:17
  • @JonSkeet , currently anything is fine , i can correct that by experimenting with different values later , or i can park it for america . – Pawan Nov 02 '15 at 14:20

1 Answers1

1

You're nearly there.

If you want to print the hh:mm format adapted to IST, you can:

if (country.equals("Australia")) {
    in.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
    SimpleDateFormat out = new SimpleDateFormat("hh:mm");
    out.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    return out.format(in.parse(time));
}
Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    Ick to manual date/time formatting - *much* better to use a new SimpleDateFormatter with the appropriate time zone set. – Jon Skeet Nov 02 '15 at 14:15
  • @JonSkeet I agree, went a little too quick & dirty there. – Mena Nov 02 '15 at 14:16
  • Thanks but its giving me wrong time (it should be 11:10 am) for the value 4:40pm in austrlaia – Pawan Nov 02 '15 at 14:17
  • Rather than using "IST" you should use the actual IANA time zone ID - "Asia/Kolkata" in this case. That *may* not be the problem, but it's definitely a good idea in general. – Jon Skeet Nov 02 '15 at 14:22
  • @JonSkeet must have missed that somewhere in translation. Note that it returns the same time as would `IST` in this instance... – Mena Nov 02 '15 at 14:23
  • @Mena , Working absolutely fine , thank you very much – Pawan Nov 02 '15 at 14:25
  • @PreethiJain you're welcome. Say thanks to Jon too, otherwise you'd have gotten some much uglier code :) – Mena Nov 02 '15 at 14:26