0

i have done code below to change time from UTC to another time zone but code is showing only UTC time.Also after formatting to source time format it shows system time zone .

private String setTimezone(String time){
sourceformatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
dateFormatter = new SimpleDateFormat("hh:mm a");

sourceformatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Log.e("reicievedformat",time);
Date value = null;
try {
    value = sourceformatter.parse(time);
} catch (ParseException e) {
    e.printStackTrace();
}
Log.d("afterfirstformat",dateFormatter.format(value));
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
time =dateFormatter.format(value);
    Log.d("Finaltime",time);
return time;
}

Output:- Log values

  • E/reicievedformat: 12:36 PM, Mon 08 Oct 2018

  • D/afterfirstformat: 06:21 PM

  • D/Finaltime: 12:36 PM

As you can see I'm getting 12:36 PM, Mon 08 Oct 2018 ("UTC") and I want to convert to IST, but the final time, 12:36 PM, doesn’t seem to have been converted.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
RIK
  • 123
  • 1
  • 2
  • 13
  • Hi Welcome to stack**overflow**. Now what is your requirement? Can you please elaborate it? – Santhosh Joseph Oct 09 '18 at 09:07
  • 1
    what's your expect output – navylover Oct 09 '18 at 09:07
  • i'm getting 12:36 PM, Mon 08 Oct 2018 ("UTC") and i want to convert to IST – RIK Oct 09 '18 at 09:10
  • https://stackoverflow.com/questions/14314426/how-to-parse-date-from-gmt-timezone-to-ist-timezone-and-vice-versa-in-android – Prasanth S Oct 09 '18 at 09:19
  • Funnily I cannot reproduce. On my Java 11 I get a final time of `06:06 PM`, which I believe is what you wanted (haven’t tried on Android). – Ole V.V. Oct 10 '18 at 08:31
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. `LocalDateTime.parse("12:36 PM, Mon 08 Oct 2018", DateTimeFormatter.ofPattern("hh:mm a, E dd MMM yyyy", Locale.ENGLISH)).atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.of("Asia/Kolkata")).format(DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH))`. – Ole V.V. Oct 10 '18 at 09:03

2 Answers2

0

IST in java stands for "Israel Standard Time". Use this for "Indian Standard Time"

dateFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));

The date & time APIs in Java gives you headache. I use this library by Daniel Lew. https://github.com/dlew/joda-time-android

rohiththammaiah
  • 153
  • 1
  • 5
  • If your system time zoneis "IST" try changing IST to another time zone and match with that. – RIK Oct 09 '18 at 09:15
  • In java IST stands for Israel Standard time – rohiththammaiah Oct 09 '18 at 09:27
  • 1
    On my Java IST is understood as Atlantic/Reykjavik (Iceland Standard or Summer Time?) Three and four letter time zone abbreviations are very often ambiguous, and it’s like a box of chocolate, you don’t know what you get… (IST, to stay with this example, may also mean Irish Standard or Summer Time). – Ole V.V. Oct 10 '18 at 05:37
0

Try this

public static String getDateOut(String ourDate) {
        try
        {
            //be sure that passing date has same format as formatter
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date value = formatter.parse(ourDate);

            SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a"); //this format changeable
            dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
            ourDate = dateFormatter.format(value);

        }
        catch (Exception e)
        {
            ourDate = "00-00-0000 00:00";
        }
        return ourDate;
    }
Arnold Brown
  • 1,330
  • 13
  • 28