Skip to content Skip to sidebar Skip to footer

Simpledateformat Changing Timezone

I have strange problem with time format conversion. I have string , time = '11:00' I have to convert above string to date and I am doing the following: Calendar cal= Calendar.getI

Solution 1:

Yes, it does affect. By default, SimpleDateFormat uses default timezone of system if none specified. Try specifying it in the method (also, SimpleDateFormat is not thread safe so don't use it as a static variable):

public static Date fromShortTime(String shortTime){
    try {
        SimpleDateFormat shortTimeFormat = new SimpleDateFormat("HH:mm");
        shortTimeFormat.setTimeZone(TimeZone.getTimeZone("PST"));
        return shortTime == null ? null : shortTimeFormat.parse(shortTime);
    } catch (java.text.ParseException e) {
        return null;
    }
}

Post a Comment for "Simpledateformat Changing Timezone"