Monday, January 29, 2024

get timezone id from display name

 import java.util.TimeZone;


public class TimeZoneExample {

    public static void main(String[] args) {

        // Set the desired time zone display name

        String timeZoneDisplayName = "IST";


        // Get the time zone ID based on the display name

        String timeZoneID = getTimeZoneIDByDisplayName(timeZoneDisplayName);


        if (timeZoneID != null) {

            System.out.println("Time zone ID for " + timeZoneDisplayName + ": " + timeZoneID);

        } else {

            System.out.println("No matching time zone found for display name: " + timeZoneDisplayName);

        }

    }


    private static String getTimeZoneIDByDisplayName(String displayName) {

        String[] availableIDs = TimeZone.getAvailableIDs();

        for (String id : availableIDs) {

            TimeZone timeZone = TimeZone.getTimeZone(id);

            if (displayName.equals(timeZone.getDisplayName())) {

                return id;

            }

        }

        return null; // Return null if no match is found

    }

}


No comments:

Post a Comment