Date and time conversion to some other Timezone in

2019-01-01 09:36发布

问题:

i have written this code to convert the current system date and time to some other timezone. I am not getting any error but i am not getting my output as expected. Like if i execute my program at a particular time.. My output is ::

The current time in India is :: Fri Feb 24 16:09:23 IST 2012

The date and time in :: Central Standard Time is :: Sat Feb 25 03:39:23 IST 2012

And the actual Time according to CST time zone is ::

Friday, 24 February 4:39:16 a.m(GMT - 6:00)

So there\'s some time gap. and i don\'t know why this is happening. Any help will be appreciated.. The code is ::

package MyPackage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Temp2 {


    public static void main(String[] args) {

        try {
            Calendar currentdate = Calendar.getInstance();
            String strdate = null;
            DateFormat formatter = new SimpleDateFormat(\"dd-MM-yyyy HH:mm:ss\");
            strdate = formatter.format(currentdate.getTime());
            TimeZone obj = TimeZone.getTimeZone(\"CST\");

            formatter.setTimeZone(obj);
            //System.out.println(strdate);
            //System.out.println(formatter.parse(strdate));
            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);
        } catch (ParseException e) {
           e.printStackTrace();
        }
    }
}

回答1:

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());


回答2:

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.



回答3:

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\".



回答4:

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;
     }


回答5:

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);