Get TimeZone from ZoneId - Java java.time

Java examples for java.time:ZoneId

Description

Get TimeZone from ZoneId

Demo Code


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

public class Main{
    public static Date dateOf(String dateStr) {
        if (null == dateStr || dateStr.trim().length() == 0)
            return null;

        try {/*from  w  w w  .  j  ava2  s .com*/
            SimpleDateFormat format = new SimpleDateFormat(
                    DatePattern.UNTIL_MINUTE.getPattern());
            format.setTimeZone(TimeZone.getTimeZone(ZoneId.of("CTT",
                    ZoneId.SHORT_IDS)));
            return format.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(dateStr + " format is illegal.");
        }
    }
}

Related Tutorials