1

I called a WCF web service in my android project. my web service returns data from sql server database. i get DateTime and Time (UTC) and get a json like below:

{"CreateDate":"\/Date(1503952200000+0330)\/","FromTime":"PT15H30M"}

I searched and read below link, with this link, i can solve DateTime problem but for Time data type, i have another problem. my problem is:

com.google.gson.JsonSyntaxException: PT15H30M time 

helping link was this.

How to solve Time data type jsonSyntaxException?

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
Fahim
  • 384
  • 5
  • 20

2 Answers2

3

PT15H30M related to ISO 8601 time intervals. That string means “fifteen and one-half hours”. It is not a time-of-day.

I think GSON doesn't support time interval.

It uses new GsonBuilder().setDateFormat(/* ... */).create();

So I would add private String FromTime; as String and parse :

java.time.Duration duration = java.time.Duration.parse(FromTime); // PT15H30M
 // duration.get(java.time.temporal.ChronoUnit.MINUTES) // whatever you want
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • Thanks for your answer but i can't understand what you said in second line ... duration object. can you more explain? Thanks again – Fahim Sep 03 '17 at 10:36
  • @Fahim suppose you want to convert JSON string to Java object. Since (I believe) you cannot parse intervals on the fly, you can only convert it to variable as String and parse it inside your Android object. I don't know where you use intervals so cannot help here – Maxim Shoustin Sep 03 '17 at 10:41
  • Thanks . yeah i save in string var and then i set in Time object. Thanks – Fahim Sep 03 '17 at 10:47
  • 2
    @Fahim Just keep in mind that a Duration is not the same as a Time. A duration is an [**amount of time**](https://stackoverflow.com/a/44676793/7605325), such as *"2 days and 3 hours"* or *"1 hour, 10 minutes and 25 seconds"*, and a Time is a time of the day, such as *10 AM* or *15:30*. Although both use the same words (hour, minutes, second, etc), they represent different concepts. –  Sep 03 '17 at 13:09
1

The Answer by Shoustin is correct. Here's more related to the context of the Question.

Count-from-epoch

The 1503952200000 is perhaps a count of milliseconds since the epoch reference date of 1970-01-01T00:00:00Z.

Offset-from-UTC

The +0330 looks like an offset-from-UTC. The problem is that we cannot tell if that means the count-from-epoch is in UTC and we should adjust three and a half hours, or if the count-from-epoch has already been adjusted by three-and-a-half hours. I will go with the former.

This is a terrible format for communicating date-time values as text. If you have any control, change to using standard ISO 8601 formatted strings.

long count = Long.parseLong( "1503952200000" ) ;
Instant instant = Instant.ofEpochMilli( count ) ;

ZoneOffset offset = ZoneOffset.of( "+0330" ) ;
OffsetDateTime odt = instant.atOffset( offset ) ;

Duration

As Shoustin explained, the "PT15H30M" is a span-of-time of fifteen hours and thirty minutes — not a time-of-day.

Duration d = Duration.parse( "PT15H30M" ) ;

You can add that to your OffsetDateTime if you want to arrive at another point on the timeline.

OffsetDateTime odtLater = odt.plus( d ) ;

See this code run live at IdeOne.com.

count: 1503952200000

instant.toString(): 2017-08-28T20:30:00Z

odt.toString(): 2017-08-29T00:00+03:30

odtLater.toString(): 2017-08-29T15:30+03:30

Invalid Iran time?

By the way, I am confused by the offset of three and a half hours. Looking at the list of current time zones, the only case of a +03:30 offset is by Asia/Tehran time zone. But that is for standard time, not the Daylight Saving Time (DST) in effect during the given August date. For the date of 2017-08-29, there is no offset of +03:30 being used anywhere on Earth.

So I suspect you have been given faulty data. I suspect the count-from-epoch was calculated separately, and the offset was glued on after-the-fact. This is an example of why your input string format is such a poor design. Stick to ISO 8601, and avoid tracking time as a count-from-epoch.

And use time zone whenever it is known, rather than a mere offset-from-UTC. An offset is only a number of hours and minutes. A time zone is a history of past, present, and future offsets in use at various points in time by a particular region. The ZonedDateTime::toString method wisely extends the ISO 8601 to append the zone name in square brackets.

ZonedDateTime zdt = ZonedDateTime.parse( "2017-08-29T15:30+04:30[Asia/Tehran]" ) ;
String output = zdt.toString() ;  

2017-08-29T15:30+04:30[Asia/Tehran]

Tip: Generally best to exchange date-time data in UTC, as Instant objects or the ISO 8601 equivalent.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154