Kotlin - convert UTC to local time

Kotlin - convert UTC to local time

By : joran
Date : October 19 2020, 08:10 PM
around this issue I'm trying to convert a UTC string date to local time, so it's in a more readable format. I have a textView that in my activity layout: , Try using Extension Functions
code :
fun String.toDate(dateFormat: String = "yyyy-MM-dd HH:mm:ss", timeZone: TimeZone = TimeZone.getTimeZone("UTC")): Date {
val parser = SimpleDateFormat(dateFormat, Locale.getDefault())
parser.timeZone = timeZone
return parser.parse(this)

fun Date.formatTo(dateFormat: String, timeZone: TimeZone = TimeZone.getDefault()): String {
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
formatter.timeZone = timeZone
return formatter.format(this)
"2018-09-10 22:01:00".toDate().formatTo("dd MMM yyyy")

Share : facebook icon twitter icon
How to convert Local time to UTC time in Delphi XE2? and how to convert it back from UTC to local time?

How to convert Local time to UTC time in Delphi XE2? and how to convert it back from UTC to local time?

By : Perez
Date : March 29 2020, 07:55 AM
wish helps you I'm using Delphi xe2 and I'm trying to store records using UTC datetime in my database and then restore it back when a client reads it in his local datetime ? any idea how to do this forth back conversion ? , This is the function that I use to convert from UTC to local.
code :
function LocalDateTimeFromUTCDateTime(const UTCDateTime: TDateTime): TDateTime;
  LocalSystemTime: TSystemTime;
  UTCSystemTime: TSystemTime;
  LocalFileTime: TFileTime;
  UTCFileTime: TFileTime;
  DateTimeToSystemTime(UTCDateTime, UTCSystemTime);
  SystemTimeToFileTime(UTCSystemTime, UTCFileTime);
  if FileTimeToLocalFileTime(UTCFileTime, LocalFileTime) 
  and FileTimeToSystemTime(LocalFileTime, LocalSystemTime) then begin
    Result := SystemTimeToDateTime(LocalSystemTime);
  end else begin
    Result := UTCDateTime;  // Default to UTC if any conversion function fails.
LocalDateTime := TTimeZone.Local.ToLocalTime(UniversalDateTime);
Val cannot be reassigned a compile time error for a local variable in fun in Kotlin

Val cannot be reassigned a compile time error for a local variable in fun in Kotlin

By : tostka
Date : March 29 2020, 07:55 AM
hop of those help? In Kotlin val declares final, read only, reference - and that is exactly what compiler error is telling you with
How to get current local date and time in Kotlin

How to get current local date and time in Kotlin

By : Tushar Patange
Date : March 29 2020, 07:55 AM
With these it helps java.util.Calendar.getInstance() represents the current time using the current locale and timezone.
You could also choose to import and use Joda-Time or one of the forks for Android.
How can I convert a Long value to date time and convert current time to Long kotlin?

How can I convert a Long value to date time and convert current time to Long kotlin?

By : buway
Date : March 29 2020, 07:55 AM
it fixes the issue The Code A can convert a long value to date value, just like 2018.01.10 , Try this, I use SimpleDataFormat.
code :
fun convertLongToTime(time: Long): String {
    val date = Date(time)
    val format = SimpleDateFormat("yyyy.MM.dd HH:mm")
    return format.format(date)

fun currentTimeToLong(): Long {
    return System.currentTimeMillis()

fun convertDateToLong(date: String): Long {
    val df = SimpleDateFormat("yyyy.MM.dd HH:mm")
    return df.parse(date).time
Convert ISO 8601 string in UTC to local time - JodaTime is adding the opposite of local time zone

Convert ISO 8601 string in UTC to local time - JodaTime is adding the opposite of local time zone

By : Jimmy Chow
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I take it step by step:
dateParser.parseDateTime("2019-02-22T01:03:23Z") gives you Feb 22 at 01:03 UTC as expected (same point in time as Feb 21 at 18:03 at offset -07:00). dateTime.toLocalDateTime() throws the UTC offset information away, so you get Feb 22 at 01:03 with no time zone information. A LocalDateTime is a date and a time of day without time zone information, so does not correspond to any point in time. dateTime.toDate() gives you the same point in time as Feb 22 at 01:03 UTC without offset or time zone information. A Date despite its name is somehow opposite a LocalDateTime: It’s a point in time but does not correspond to any specific hour of day. "UTC: " + dateTime.toDate() appends the Date to a string. To do this, Date.toString is implicitly called to obtain a string to append to the first string. Date.toString uses you local time zone for rendering the string (this behavior confuses many). So the point in time you got is printed as Thu Feb 21 18:03:23 MST 2019, which is the correct point in time, only converted to Mountain Standard Time. localDateTime.toDate() is problematic. You’re saying that you want to convert a date and time to a point in time. LocalDateTime solves this by using your local time zone, so you get a point in time equal to Feb 22 at 01:03 MST (same point as 08:03 UTC). "Local: " + localDateTime.toDate() again calls toString and uses MST for rendering the string.
code :
    String systemUtcString = "2019-02-22T01:03:23Z";
    Instant pointInTime = Instant.parse(systemUtcString);
    ZonedDateTime denverDateTime = pointInTime.atZone(ZoneId.of("America/Denver"));
    Date oldfashionedDateObject = Date.from(pointInTime);
Related Posts Related Posts :
  • How to employ keyed-hash message authentication code (HMAC) with Android Keystore
  • Prevent inviting existing users or previously invited
  • How to overwrite windowSoftInputMode using edit-config tag in cordova
  • Difference between Loader and AsyncTask
  • In Firebase Android, retrieve the value of a child node and save it in a boolean variable
  • when i scroll the custom list in my android app then the favorite icon which is part of the custom adapter changes on it
  • adding room dependency gives manifest merge error
  • Visibility of RelativeLayout change on scrolling Gridview
  • Strange LiveData behavior vs ObservableField
  • Which layout is used for clash of clans home screen? We can scroll it horizontal as well as vertical. Please illuminate
  • RxJava: Know when observable is finished and return Boolean Observable to the caller
  • how to use savedInstanceState to save camera information when the orientation change occure
  • Style React-Native via Styles.xml
  • Keyboard won't show after calling InputMethodManager.showSoftInput
  • Compiling ffmpeg for Android on OSX
  • Button does not get clicked on the first attempt
  • AR Core performance, learning curve and comparison with other frameworks
  • Codename one : Attaching images to email message fails
  • Error: Unsupported type 'viewItem' in Android build
  • setLayoutParams on ConstraintLayout does not change size
  • How to profile the fetch time of the android gradle libraries
  • Kotlin setOnclickListener
  • Naming convention for methods returning RxJava's Completable
  • Conv3D not working
  • FFmpeg audio video merge command
  • Do you have to use gradle-experimental for JNI?
  • How to get key hashes for facebook for facebook app?
  • ZXing not calling onActivityResult, When scanning QR Code
  • Integration of Android Native module with existing Xamarin Native Project
  • Restore RecyclerView state after rotating
  • ViewModelProviders java.lang.RuntimeException trying to instantiate class which inherit AndroidViewModel
  • SKD19 vs SDK24 - Usage of API documented as @since 1.8+
  • filter option in list view fragment activity
  • Picture from camera not showing up in second activity
  • How to change imageview according to screen size
  • How to copy Android Studio project to remote Git (NOT GitHub) server
  • How to get React-Native to announce specific accessibility message via AccessibiliyManager?
  • Change colors of com.tech.freak.wizardpager.ui.StepPagerStrip
  • Android fullscreen activity shows ActionBar
  • Turn on the screen when timer calls onFinish()
  • Would like a bit of clarification with SQLite database's onUpgrade method
  • Use ObjectBox with Libgdx
  • How to make combine Gregorian and Hijri calendar in Android?
  • Try to read text file from sdcard in other language
  • "App not installed" message when installed apk
  • How to execute a designated test suite class in Firebase Test Lab
  • play music with background service
  • background not visible on LG G3
  • should every View have an id?
  • Realm object server sync on low connection.
  • Correct way to add more views to adapter
  • Getting error in android support library
  • Instant app zip uploading error
  • "No emulator installed" error in Android Studio even though Intel HAXM is up and running
  • AsyncTask without leaks
  • SQLiteException : No such table found whereas a table was created
  • Using @Component.Builder with constructor params
  • No injector was found for fragment dagger 2.11
  • Android Volley Https SSL self signed and Google Maps API
  • Why does the onClickListener not work anymore in this example?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org