Sarah Maher
Sarah Maher

Reputation: 830

Date to local time kotlin

I have a date object with

"Sun Apr 26 11:44:00 GMT+03:00 2020"

I tried to format it but it will always ignore the timezone "+3:00" and show the hour without changing it

what I have tried :

   val sdf = SimpleDateFormat("dd MM yyyy HH:mm:ss", Locale.US)
    return sdf.format(this)

Upvotes: 2

Views: 4314

Answers (3)

deHaar
deHaar

Reputation: 18568

If you just want to parse a datetime String with the given pattern and then convert the result to a different offset, you can go like this:

fun main() {
    // provide the source String
    val datetime = "Sun Apr 26 11:44:00 GMT+03:00 2020"
    // provide a pattern for parsing
    val parsePattern = "E MMM dd HH:mm:ss O yyyy";
    // parse the String to an OffsetDateTime
    val parsedOffsetDateTime = java.time.OffsetDateTime.parse(
                                datetime,
                                java.time.format.DateTimeFormatter.ofPattern(parsePattern))
    // print the result using the default format
    println(parsedOffsetDateTime)

    // then get the same moment in time at a different offset
    val adjustedOffsetDateTime = parsedOffsetDateTime.withOffsetSameInstant(
                                    java.time.ZoneOffset.of("+02:00"))
    // and print that, too, in order to see the difference
    println(adjustedOffsetDateTime)
}

which produces the output

2020-04-26T11:44+03:00
2020-04-26T10:44+02:00

Upvotes: 2

Andreas
Andreas

Reputation: 159086

The format string depends on which SimpleDateFormat you use:

Demo (in plain Java)

TimeZone.setDefault(TimeZone.getTimeZone("Asia/Jerusalem"));

Date date = new Date(1587890640000L); // date object with "Sun Apr 26 11:44:00 GMT+03:00 2020"

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'XXX yyyy", Locale.US);
String dateString = sdf.format(date);
System.out.println("Formatted  : " + dateString);

Date parsed = sdf.parse(dateString);
System.out.println("Parsed back: " + parsed);
System.out.println("Millis since Epoch: " + parsed.getTime());

Output

Formatted  : Sun Apr 26 11:44:00 GMT+03:00 2020
Parsed back: Sun Apr 26 11:44:00 IDT 2020
Millis since Epoch: 1587890640000

Upvotes: 1

kaustubhpatange
kaustubhpatange

Reputation: 532

I don't exactly understand your question but I guess you meant you need DateTime related to a particular zone. In kotlin we've ZonedDateTime class

import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

val londonZone = ZoneId.of("Europe/London")
val londonCurrentDateTime = ZonedDateTime.now(londonZone)
println(londonCurrentDateTime) // Output:  2018-01-25T07:41:02.296Z[Europe/London]
val londonDateAndTime = londonCurrentDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM))
println(londonDateAndTime) // Output: Thursday, January 25, 2018 7:40:34 AM

You also might need to take a look at LocalDateTime class.

Upvotes: 5

Related Questions