TimeZone validation in Java

前端 未结 9 2274
梦如初夏
梦如初夏 2021-02-20 00:08

I have a string, I need to check whether it is a standard time zone identifier or not. I am not sure which method I need to use.

String timeZoneToCheck = \"UTC\"         


        
9条回答
  •  心在旅途
    2021-02-20 00:18

    it's simple just use ZoneId.of("Asia/Kolkata")

    try {
        ZoneId.of("Asia/Kolkataa");
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    if your insert invalid time zone it will throw exception

    **java.time.zone.ZoneRulesException: Unknown time-zone ID: Asia/Kolkataa**
    

提交回复
热议问题