Date and time conversion to some other Timezone in java

后端 未结 10 469
失恋的感觉
失恋的感觉 2020-12-11 21:13

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

相关标签:
10条回答
  • 2020-12-11 21:44

    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;
         }
    
    0 讨论(0)
  • 2020-12-11 21:45

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

    0 讨论(0)
  • 2020-12-11 21:45

    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);
    
    0 讨论(0)
  • 2020-12-11 21:46

    SimpleDateFormat#setTimezone() is the answer. One formatter with ETC timezone you use for parsing, another with UTC for producing output string:

    DateFormat dfNy = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
    dfNy.setTimeZone(TimeZone.getTimeZone("EST"));
    DateFormat dfUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
    dfUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
    
    try {
        return dfUtc.format(dfNy.parse(input));
    } catch (ParseException e) {
        return null;              // invalid input
    }
    
    0 讨论(0)
  • 2020-12-11 21:52

    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());
    
    0 讨论(0)
  • 2020-12-11 21:54

    You can use two SimpleDateFormat, one for parse the date string with EST timezone, one for print the date with UTC timezone

    String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat estFormatter = new SimpleDateFormat(format);
        estFormatter.setTimeZone(TimeZone.getTimeZone("EST"));
        Date date = estFormatter.parse("2015-11-01 01:00:00");
    
        SimpleDateFormat utcFormatter = new SimpleDateFormat(format);
        utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    
        System.out.println(utcFormatter.format(date));
    
    0 讨论(0)
提交回复
热议问题