0

I want to display time like this

09/07/18, 23:15:00 IST

I use above timeformat but instance of show timezone like this IST its show GMT+05:30. I use below code to parse a date :-

 fun parseDate(time: String?): String? {
    if (time != null) {
        val inputPattern = "yyyy-MM-dd'T'HH:mm:ss"
        val outputPattern = "MM/dd/yy, HH:mm:ss zzz"
        val inputFormat = SimpleDateFormat(inputPattern, Locale.getDefault())
        val outputFormat = SimpleDateFormat(outputPattern, Locale.getDefault())

        var date: Date? = null
        var str: String? = null

        try {
            date = inputFormat.parse(time)
            str = outputFormat.format(date)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return str
    }

    return time
}

Can any one help me to getting out this problem.

Anand Jain
  • 2,365
  • 7
  • 40
  • 66
  • 1
    As an aside, even on Android consider not using the long outdated and notoriously `SimpleDateFormat` class and adding [the ThreetenABP library](https://github.com/JakeWharton/ThreeTenABP) to your project so you may use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Sep 08 '18 at 20:00
  • Possible duplicate of [Java How to get time printed in IST](https://stackoverflow.com/questions/10714103/java-how-to-get-time-printed-in-ist) – Ole V.V. Sep 08 '18 at 20:10

3 Answers3

0

You have to set the time zone you want like this

outputFormat.timeZone =  TimeZone.getTimeZone("Asia/Calcutta") // this is a unique identifier of "IST" 

Example: https://try.kotlinlang.org/#/UserProjects/ulc8sg2dslsbgt4qsauqd1a1sv/akvvttd16f1sqjcg6rm7smuh6p

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • for me everything less than 4 z prints UTC https://try.kotlinlang.org/#/UserProjects/ulc8sg2dslsbgt4qsauqd1a1sv/akvvttd16f1sqjcg6rm7smuh6p prints `09/07/18, 12:51:53 UTC` – leonardkraemer Sep 07 '18 at 12:52
  • 1
    Never rely on three letter time zone abbreviations. You won’t know whether you get Irish Summer Time, Israel Standard Time or India Standard Time. Instead use Europe/Dublin, AsiaTel_Aviv, Asia/Jerusalem or Asia/Kolkata. – Ole V.V. Sep 08 '18 at 20:03
  • 1
    I would have never thought that these codes were not unique. Thanks for pointing it out. – leonardkraemer Sep 08 '18 at 21:10
  • But as far as I understand from [TimeZone Source](http://developer.classpath.org/doc/java/util/TimeZone-source.html) IST defaults to indian standard time in java.util.timezone and the specific alias should be "Asia/Calcutta" – leonardkraemer Sep 08 '18 at 21:17
  • 1
    While on my computer `TimeZone.getTimeZone("IST")` does give India Standard Time, `ZonedDateTime.parse("2018-09-10 12:00:00 IST", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss z"))` parses into Asia/Jerusalem on my Java 8 and into Atlantic/Reykjavik on my Java 10. Please avoid the confusion and use the region/city format for time zones. For you own sake and for the sake of those reading and maintaining your code after you – Ole V.V. Sep 10 '18 at 07:28
  • That is very interesting and very weird. Especially because Atlantic/Reykjavik != Irish Standard Time or any other Timezone with the Name IST denoted at https://www.timeanddate.com/time/zones/ist . Probably one of the reasons why java.util.time was deprecated. – leonardkraemer Sep 10 '18 at 10:16
0

According to the docs it should be: "MM/dd/yy, HH:mm:ss zz". This is two 'z' instead of 3.

And from this other question you have to set the TimeZone on the SimpleDateFormat.

z: Pacific Standard Time
zz: PST
zzz: GMT-08:00

But as the OP says the three alternatives give the same output.

Example:

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

public class HelloWorld{

 public static void main(String []args){
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy, HH:mm:ss zz");
    Date date = new Date();
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));
    String txtDate = sdf.format(date);
    System.out.println(txtDate);

    sdf = new SimpleDateFormat("MM/dd/yy, HH:mm:ss z");
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));
    txtDate = sdf.format(date);
    System.out.println(txtDate);

    sdf = new SimpleDateFormat("MM/dd/yy, HH:mm:ss zzz");
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));
    txtDate = sdf.format(date);
    System.out.println(txtDate);
 }
}

Output:

09/07/18, 18:26:08 IST
09/07/18, 18:26:08 IST
09/07/18, 18:26:08 IST

SimpleDateFormat formats

Juan
  • 5,525
  • 2
  • 15
  • 26
0

As per Document https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html , you have to use 'z' instead of 'zzz and also try to set time zone for your dateformats like below.

 inputFormat.timeZone = TimeZone.getTimeZone("IST")
Naveen
  • 350
  • 2
  • 12
  • 3
    Never rely on three letter time zone abbreviations. You won’t know whether you get Irish Summer Time, Israel Standard Time or India Standard Time. Instead use Europe/Dublin, AsiaTel_Aviv, Asia/Jerusalem or Asia/Kolkata. – Ole V.V. Sep 08 '18 at 20:02