Frage

Ich habe diesen Code geschrieben, um das aktuelle Systemdatum und die Uhrzeit in eine andere Zeitzone umzuwandeln.Ich bekomme keinen Fehler, aber ich bekomme meine Leistung nicht wie erwartet.Wenn ich mein Programm zu einem bestimmten Zeitpunkt ausführe. Meine Ausgabe lautet ::

Die aktuelle Zeit in indien lautet :: Fri Feb 24 16:09:23 IST 2012

Datum und Uhrzeit in :: zentrale Standardzeit ist :: Sat Feb 25 03:39:23 IST 2012

und die eigentliche zeit nach cst zeitzone lautet :: generasacodicetagpre.

also gibt es ein paar Zeitlücke .Und ich weiß nicht, warum das passiert.Jede Hilfe wird geschätzt. Der Code lautet :: generasacodicetagpre.

War es hilfreich?

Lösung

It's over the web. Could have googled. Anyways, here is a version for you (shamelessly picked and modified from here):

Calendar calendar = Calendar.getInstance();
TimeZone fromTimeZone = calendar.getTimeZone();
TimeZone toTimeZone = TimeZone.getTimeZone("CST");

calendar.setTimeZone(fromTimeZone);
calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
if (fromTimeZone.inDaylightTime(calendar.getTime())) {
    calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
}

calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
if (toTimeZone.inDaylightTime(calendar.getTime())) {
    calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
}

System.out.println(calendar.getTime());

Andere Tipps

Your mistake is to call parse instead of format.

You call parse to parse a Date from a String, but in your case you've got a Date and need to format it using the correct Timezone.

Replace your code with

Calendar currentdate = Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
TimeZone obj = TimeZone.getTimeZone("CST");
formatter.setTimeZone(obj);
System.out.println("Local:: " +currentdate.getTime());
System.out.println("CST:: "+ formatter.format(currentdate.getTime()));

and I hope you'll get the output you are expecting.

Handling dates in Java in my daily work is a non-trivial task. I suggest you to use Joda-Time that simplify our coding days and you don't have to "re-invent the wheel".

You can just use "CST6CDT" because in some countries they follow CDT in summer and CST in winter

 public static String getDateInCST() {
             Calendar calendar = Calendar.getInstance();
             DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
             formatter.setTimeZone(TimeZone.getTimeZone( "CST6CDT"));
             String strdate = formatter.format(calendar.getTime());
             TimeZone.getAvailableIDs();
             return strdate;
     }

Problem is when you print date obj it call toString method and it will print in your machines default time zone. Try this code and see difference.

Calendar currentdate = Calendar.getInstance();
String strdate = null;
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ssz");
strdate = formatter.format(currentdate.getTime());
System.out.println("strdate=>" + strdate);
TimeZone obj = TimeZone.getTimeZone("CST");

formatter.setTimeZone(obj);
strdate = formatter.format(currentdate.getTime());
Date theResult = formatter.parse(strdate);

System.out.println("The current time in India is  :: " +currentdate.getTime());

System.out.println("The date and time in :: " + obj.getDisplayName() + "is ::" + theResult);
System.out.println("The date and time in :: " + obj.getDisplayName() + "is ::" + strdate);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top