0

So, I have 2 devices in 2 locations. This is how I'm parsing the time I get from a custom message object:

long mtime = message.getEpochTime();
Date messageTime = new Date( mtime * 1000 );
String dateString = messageTime.toString();
String datePrint = dateString.substring(0, dateString.length()-12);
timeTextView.setText(datePrint);

The dates and times are correct in both locations (adjusted to local time zone). However, the formats are different:

In one phone the format is Thu Jun 22 22:31 and the other shows the time as Fri Jun 23 08:31:52 GM

Why is this happening? The length of the datestring should be the same in both locations.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
McFiddlyWiddly
  • 547
  • 10
  • 29
  • 1
    Because different locales format dates differently. Try [Japan](https://stackoverflow.com/questions/20174225/convert-japans-date-format-2013%E5%B9%B411%E6%9C%8824%E6%97%A5-to-normal-date-format-2013-11-24) (for example), – Elliott Frisch Jun 23 '17 at 03:16
  • Did you try your search engine? Did you find [All about java.util.Date](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/), for example? – Ole V.V. Jun 23 '17 at 04:14
  • It’s funny that one device leaves the seconds out, the other one shows them. – Ole V.V. Jun 23 '17 at 04:20
  • [The documentatation of `Date.toString()`](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString()) says that it should produce the form `dow mon dd hh:mm:ss zzz yyyy`, so leaving out the seconds seems to be incorrect. Unless I am missing some Android-specific documentation that allows it. – Ole V.V. Jun 23 '17 at 04:23
  • 1
    Why are you chopping 12 chars off the string you are showing us? When you don’t show us it all, it’s hard to tell exactly what is happening and in particularly why the strings have the lengths they have. – Ole V.V. Jun 23 '17 at 04:37

2 Answers2

1

You can set any format for display date you want. Example:

SimpleDateFormat ENGLISH_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd",
            Locale.ENGLISH);
String datePrint = ENGLISH_DATE_FORMAT.format(messageTime);
timeTextView.setText(datePrint);
DucPV
  • 21
  • 2
1

The documentatation of Date.toString() says that it should produce the form dow mon dd hh:mm:ss zzz yyyy.

zzz is the time zone. It doesn’t say which time zone it uses, and a Date object doesn’t have a time zone in it. Common experience is that it usually uses the default time zone of the device. This even means that you can change the time zone setting of your device and the same Date object will then display differently. This comes as a surprise to many.

According to the documentation leaving out the seconds, as one of your devices does, seems to be incorrect.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161