0
val startHour = SimpleDateFormat("HH:mm").format(pickup.begin())

The pickup.begin value is "Wed Apr 10 10:00:00 GMT+03:00 2019", so I need the start hour to be 10:00 +3h -> 13:00, but I get startHour value of 10:00.

I don't know how to add the GMT value to hour.

ghita
  • 2,746
  • 7
  • 30
  • 54
  • What is the type of `pickup` ? – Arthur Attout Apr 10 '19 at 08:13
  • Sorry, what is the type returned by `pickup.begin()` * ? – Arthur Attout Apr 10 '19 at 08:20
  • `final @Nullable Object begin; /** * The datetime the range begins. */ public @Nullable Object begin() { return this.begin; }` . it is from a generated class – ghita Apr 10 '19 at 08:30
  • you need to convert it to your local time – primo Apr 10 '19 at 08:30
  • I need to show the hour from server + GMT from the response (in that case +3,but it may vary) – ghita Apr 10 '19 at 08:32
  • @ArthurAttout I've looked now in debug, the `pickup.begin()` returns a `Date` object – ghita Apr 10 '19 at 09:08
  • 2
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, 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 10 '19 at 11:04

3 Answers3

3

No, you’ve misunderstood. Wed Apr 10 10:00:00 GMT+03:00 2019 is (the textual representation of) a java.util.Date the value of which equals 2019-04-10T07:00 UTC. It seems your default time zone is GMT+03:00, and Date is trying to be friendly to you and print the time in this time zone, which is why it prints 10:00:00. 13:00 would certainly be incorrect no matter if you wanted the time in UTC or in your own default time zone.

The Date class returned from pickup.begin() is poorly designed and long outdated, so you may want to consider if a type from java.time, the modern Java date and time API, could be returned instead. It may also make the matter clearer.

Alternatively, convert that java.util.Date object to its modern counterpart, a java.time.Instant. Look for new conversion methods added to the old classes.

Instant instant = pickup.begin().toInstant() ;  // Converting legacy `Date` object to modern `Instant` object. 

Search Stack Overflow and read the Oracle Tutorial to learn more about Instant, OffsetDateTime, and ZonedDateTime classes.

You can use java.time on older Android versions if you add ThreeTenABP to your Android project. It’s the Android adaptation of the backport of java.time.

Links

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Try adding a Locale to your simpledateformat, matching the +3 timezone.

val startHour = SimpleDateFormat("HH:mm", Locale.Germany).format(pickup.begin())
Joachim Haglund
  • 775
  • 5
  • 15
0

You can try something like this

I take 'date' as string here

  String date="something" 
 val sdf = SimpleDateFormat("HH:mm")
 sdf.timeZone = TimeZone.getTimeZone("UTC")
 val gmt = sdf.parse(date)

If this is your case

Date date="somevalue"

then

val gmt = sdf.format(date)

gmt will return you your local time

primo
  • 1,340
  • 3
  • 12
  • 40