Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Changing timezone of the date to UTC on API Level 21

Posted on November 11, 2022 By

Solution 1 :

Try this:

public Date getDateInUtc() {
        String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        Date dateToReturn = null;

        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
        String utcTime = sdf.format(new Date());

        try {
            dateToReturn = dateFormat.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateToReturn;
    }

Solution 2 :

Use cal.getTime(); which returns date

Solution 3 :

The same result you can get without SimpleDateFormat:

public Date dateInUtc(Date input) {
    Calendar inputCalendar = Calendar.getInstance();
    inputCalendar.setTime(input);
    TimeZone timeZone = inputCalendar.getTimeZone();
    long timeInUtc = input.getTime() - timeZone.getRawOffset();

    Calendar outputCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    outputCalendar.setTimeInMillis(timeInUtc);
    return outputCalendar.getTime();
}

Problem :

I have a Date and I need to change timezone of this date to UTC. The code below does not work.

    Calendar cal = Calendar.getInstance();

    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    cal.setTimeInMillis(dateLocal.getTime());

    return new Date(cal.getTimeInMillis());

On the Stackoverflow all examples return either a String or use API 26. How can I solve my problem on the Android API 21?

Comments

Comment posted by Mark Rotteveel

A

Comment posted by Andrew Efremov

timezone doesn’t change

READ  [FIXED] android - How do I save a session for a specific time?
Powered by Inline Related Posts
Android Tags:android, datetime, java

Post navigation

Previous Post: Can a beginner learn Moonlight Sonata 1st Movement?
Next Post: [FIXED] java – App keeps crashing when ListView item is clicked on

Related Posts

[FIXED] android – How to Pass Value between pages React Native Android
[FIXED] Android Studio, how to see “tips(quick documentation)”? Android
[FIXED] xamarin.forms – Listview is scrolling on iOS but not Android Android
[FIXED] java – How can I retrieve a users username from Firebase Realtime database for a welcome back message? Android
[FIXED] automation – Appium- node.js – I am using this touch function and want to tap on this specific coordinate.But it gives me an error Android
[FIXED] android – what is the correct way to make reusable xml layout for my fragments? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme