2

I have a text view that should show date time as follows on it.

2018-04-25T12:40:15 IST

I tried with simple unit test in android studio, it print the exact result what i want

My Test Class

  public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
    Date now = Calendar.getInstance().getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z");
    sdf.setTimeZone(TimeZone.getDefault());
    System.out.println(sdf.format(now) );
}
}

Output

 2018-04-25T12:40:15 IST

But when I show it on a TextView in Activity it is showing something different as follows

2018-04-25T12:31:29 GMT+05:30

I don't know what mistake I have made, Can any one help me how to achieve. Also note IST is not static for all result, It should be dynamic depends on the users time zome.

Anbarasu Chinna
  • 975
  • 9
  • 28
  • No instead of GMT+05:30 I need it to be IST or EST or something else. Depends on timezone – Anbarasu Chinna Apr 25 '18 at 08:02
  • That's exactly what Jon Skeet did in the answer – Anuraag Baishya Apr 25 '18 at 08:02
  • I tried above link it does not print IST at the end of the string output – Anbarasu Chinna Apr 25 '18 at 08:04
  • `2018-04-25T13:39:06 IST` I got this output using your code – Anuraag Baishya Apr 25 '18 at 08:09
  • if you run this in computer it is printing IST... Move that code to android activity and run it. The result is different. – Anbarasu Chinna Apr 25 '18 at 08:10
  • If the time zone setting of your device is `GMT+05:30`, then you get `GMT+05:30` when using `TimeZone.getDefault()`. The principle is called *garbage in garbage out*. As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and its equally outdated friends `TimeZone`, `Calendar` and `Date`; 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. – Ole V.V. Apr 25 '18 at 12:06
  • oh..ok i will try that – Anbarasu Chinna Apr 25 '18 at 12:08
  • It would be nice if you could convert an offset (like +05:30) automatically into a time zone (and then use the time zone for formatting the date-time so you could get `IST`). Unfortunately that’s not possible, as has been said in a number of Stack Overflow answers already. See for exmple [How to get timezone from timezone offset in java?](https://stackoverflow.com/questions/37184908/how-to-get-timezone-from-timezone-offset-in-java) – Ole V.V. Apr 25 '18 at 13:04

3 Answers3

1

I'd go for Anuraag's answer and show the full timezone name.

Abbreviations like IST and EST are not ideal because they are non-standardized and not even unique, see this answer for a more detailed explanation: https://stackoverflow.com/a/18407231

Example: IST is used by India, Israel and Ireland, and you can't know which one it is. EST is used by USA and Australia, and so on.

Of course for most cases the user will know it refers to the place they're in - an user in India using a device configured with India's timezone will show IST and the user will know it's correct. But what if this same user travels to Ireland or Israel and don't change the device's settings? I guess it'll probably show "IST", but using the country's local time, which might be confusing. I'd prefer to show the full timezone ID and eliminate the ambiguity.

whatsupnow
  • 11
  • 1
  • It doesn’t really answer how you get rid of `GMT+05:30` on a device with `GMT+05:30` time zone setting (which is not a true time zone, but nevertheless selectable as time zone on an Android device). Great advice anyway. – Ole V.V. Apr 25 '18 at 12:13
  • If the device is set to "GMT+05:30", and you want to use device's default config, I'm not sure there's a way to get rid of it. The only ways I see is to use a specific timezone instead of the default, or change the default, but that's not what the asker wants. – whatsupnow Apr 25 '18 at 12:20
0
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
    Date now = Calendar.getInstance().getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z");
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));
    System.out.println(sdf.format(now) );
}
}

Try above code may be it will help you

shekhar pande
  • 1,172
  • 1
  • 11
  • 21
  • No instead of GMT+05:30 I need it to be IST or EST or something else. Depends on timezone – Anbarasu Chinna Apr 25 '18 at 08:02
  • Also don’t base your code on a three letter time zone abbreviation. Your code will give Israel Standard Time on one device, India Standard Time on another, and who knows, Irish Summer Time on a third. – Ole V.V. Apr 25 '18 at 12:10
0

From Android TimeZone Documentation:

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

IST is not amongst the three-letter time zones that are supported. You can get a complete list of supported IDs using TimeZone.getAvailableIDs()

As a workaround, you can show the TimeZone in this form: US/Alaska, US/Aleutian, Asia/Kolkata, Asia/Istanbul etc

For this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", current);
TimeZone timeZone = TimeZone.getDefault();
sdf.setTimeZone(timeZone);
String date = sdf.format(now) + " " + timeZone.getID();

where String date will have value like

2018-04-25T14:12:57 Asia/Kolkata

Not sure if this will help you.

Anuraag Baishya
  • 874
  • 2
  • 11
  • 26