-4
String time1= "12:45 PM"; Device1 default format ,
String time2= "12:45 p.m."; Device2 default format  

These two times are same but looks different format in different devices. Testing with remi note4 (time1 format) and Samsung j7 (time2 format). If I'm using device with default time format 'time1' then i cant convert string format 'time2' to timestamp. Logger shows unable parse date . How to resolve this problem? NB: Time to be converted is fetching from server as JSON string format.

piet.t
  • 11,718
  • 21
  • 43
  • 52
SARATH V
  • 500
  • 1
  • 7
  • 33
  • Probably this is related to each device's default locale. Check that using `java.util.Locale` class, see the result of `Locale.getDefault()` on each device, they'll probably be different, so you need to parse and format each one based on their locales. Also, it's not clear to me what's the value you receive from server and to what format you want to convert. –  Jul 19 '17 at 11:48
  • converting to 'long' type timestamp. I got one more doubt.. if i convert both string type to timestamp with same time, are timestamps will be same? – SARATH V Jul 20 '17 at 05:09
  • If this `long` timestamp is "the number of milliseconds from epoch (`1907-01-01T00:00Z`", then you need all date and time fields (day, monty, year, hour, minutes, seconds (optionally fraction of seconds) and the timezone). With **only** hour and minute, you can't get this timestamp value. –  Jul 20 '17 at 11:58
  • Another improved question and answer resolved issue. [Problem solved. Click here for reference](https://stackoverflow.com/questions/45237783/time-parsing-issue-on-android) – SARATH V Aug 10 '17 at 09:44

2 Answers2

1

You can first convert it to same format then you will parse like :

String time1= "12:45 PM";
String time2= "12:45 p.m.";

String modify_time_2 = time2.replace("p.m.","PM").replace("pm","PM").replace("p m","PM");

You can use this modify_time_2 for converting it to time.

Manoj Srivastava
  • 670
  • 5
  • 16
-1

Another question of mine got the exact answer for the following issue. Please refer following link if anyone wants to know..Refer following link

SARATH V
  • 500
  • 1
  • 7
  • 33