Lokale Datum & Uhrzeit in UTC und dann von UTC in lokale Datum & Zeit

Ich versuche zu konvertieren locale Zeit auf UTC, und dann UTC-die locale Zeit. Aber ich bin nicht immer das Ergebnis.

public class DateDemo
{

public static void main(String args[])
{
    DateFormat dateFormatter = 
               DateFormat.getDateTimeInstance
                 (DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());

    TimeZone.setDefault(TimeZone.getDefault());

    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");

    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);

    try
    {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String date = simpleDateFormatter.format(today);
        String time = simpleTimeFormatter.format(today);
        System.out.println("Today's only date: " + date);
        System.out.println("Today's only time: " + time);

        ////Locale to UTC converting

        simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        simpleTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = simpleTimeFormatter.format(today);
        System.out.println("Convert into UTC's date: " + utcDate);
        System.out.println("Convert into UTC's only time: " + utcTime);

         ////UTC to locale converting

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = simpleTimeFormatter.parse(utcTime);

        String getLocalDate = simpleDateFormatter.format(getDate);
        String getLocalTime = simpleTimeFormatter.format(getTime);
        System.out.println("Get local date: " + getLocalDate);
        System.out.println("Get local time: " + getLocalTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }
  }
 }

Ich schicke lokale Datum & Zeit, um die web-service -, und dann, wenn erforderlich, die ich brauche zum abrufen der UTC-Datum & Zeit und konvertieren Sie dann in locale Datum & Zeit (D. H. Benutzer, lokale Einstellungen).

Beispiel-Ausgabe:

Locale:11/9/12 8:15 PM
After parsing a date: Fri Nov 09 20:15:00 SGT 2012
Today's only date: 09/11/2012
Today's only time: 08:15:30 PM
Convert into UTC's date: 09/11/2012
Convert into UTC's only time: 12:15:30 PM
Get local date: 09/11/2012
Get local time: 12:15:30 PM

Nach Saksak & ADTC Antworten:

Für das code-fragment, UTC-Datum & Zeit (was wird tatsächlich kommen, wie GMT-5, weil die Datenbank möglicherweise in den USA) ist der Eingang, und ich will so lokale Datum & Zeit, die als Ausgabe. Aber das folgende segment ist immer noch die GMT-5 Zeit.

SimpleDateFormat simpleDateTimeFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

....

Date inDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("inTime")); Date outDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("outTime"));

simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault()); simpleDateTimeFormatter.setTimeZone(simpleDateTimeFormatter.getTimeZone());

//TimeZone tzTimeZone = TimeZone.getDefault();

//System.out.println("Current time zone: " + tzTimeZone.getDisplayName());

String getLocalInTimeString = simpleDateTimeFormatter.format(inDateTime); 

String getLocalOutTimeString = simpleDateTimeFormatter.format(outDateTime);

Meine Frage: getLocalInTimeString & getLocalOutTimeString noch zeigen GMT-5 timing. Was ist hier falsch? Muss ich irgendwelche anderen Dinge?

  • Was genau funktioniert nicht? Ausnahmen? Stacktraces? Ausgabe nach der Ausführung des Programms?
  • Share yours-Code, wird der sound für unser Effektives Gespräch..
  • beachten Sie, dass die Verwendung von zwei separate Parser für das Datum und die Zeit verursachen kann-Konvertierung Probleme. Um Sie zu vermeiden, verwenden Sie einen parser zum Parsen von Datum und Zeit zusammen. Bitte siehe meine Antwort für weitere details.
InformationsquelleAutor Dibyendu | 2012-11-09
Schreibe einen Kommentar