0

I'm getting a date and a timezone as a string. i want to convert these in a calendar object as per the given timezone. For that i have written below code :

package com.test.practice;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
public class PS1 {

    public static void main(String[] args) throws ParseException {

        String date1 = "2018-03-26T06:00:00Z";
        String timeZone = "GMT+02:00";
        Calendar cal1 = convertStringToDate(date1);
        Calendar cal2 = convertStringToDateWithTimeZone(date1,timeZone);
        System.out.println("cal1 is "+cal1.getTime());
        System.out.println("cal2 is "+cal2.getTime());

    }

    public static Calendar convertStringToDate(final String date) throws ParseException {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        final Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(date));
        return cal;
    }

       public static Calendar convertStringToDateWithTimeZone(final String date,String timezone) throws ParseException {
        final Calendar cal =  Calendar.getInstance();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        sdf.setTimeZone(TimeZone.getTimeZone(timezone));
        cal.setTime(sdf.parse(date));
        return cal;
    }

}

i'm trying to print calendar object as per timezone and without timezone but in both case i'm getting same result. here is output

cal1 is Mon Mar 26 11:30:00 IST 2018
cal2is Mon Mar 26 11:30:00 IST 2018

I refer below links but didn't get any help.

Convert Date/Time for given Timezone - java https://www.journaldev.com/696/how-to-convert-java-date-into-specific-timezone-format Date and time conversion to some other Timezone in java

Can anyone help regarding this.

user2142786
  • 1,484
  • 9
  • 41
  • 74
  • 1
    A `Date` cannot have a time zone in it. – Ole V.V. Apr 11 '18 at 06:51
  • 2
    You're printing the result of `Date.toString()` - a `Date` doesn't have a time zone. See https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/ – Jon Skeet Apr 11 '18 at 06:51
  • How about using Joda-Time instead of Calender or Date? They have tons of Bugs and Error and also, developer admitted it. – TyeolRik Apr 11 '18 at 06:53
  • You are returning a calendar object. In both the links that you have mentioned, the return type is a string which is fetched from the date object. So you need to convert cal to date and then to string – Tridev Chaudhary Apr 11 '18 at 06:53
  • 1
    I recommend you avoid the outdated classes `Calendar`, `SimpleDateFormat` and `TimeZone`. They’re troublesome and confusing. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). An `OffsetDateTime` is a date and time with an offset, I suppose this is what you need (since `GMT+02:00` is an offset, not really a time zone). – Ole V.V. Apr 11 '18 at 06:57

0 Answers0