简体   繁体   中英

Setting up locale in Java Gregorian Calendar

Java API refers to IANA Language Subtag Registry (under region) to look up for country code. In IANA websiste, the region code for United Kingdom is GB. However, setting up a GregorianCalendar object using GB:

import java.util.*;

class MainClass{
    public static void main (String[] args) {

        GregorianCalendar date1 = new GregorianCalendar(Locale.GB);
        int year = date1.get(GregorianCalendar.YEAR);
        int weekday = date1.get(GregorianCalendar.DAY_OF_WEEK);

        System.out.println(year);
        System.out.println(weekday);
    }
}

resulted in the following error message:

cannot find symbol
symbol:   variable GB
location: class locale

What should I do instead, please?

尝试使用Locale.UK以使您的代码得以编译。

The constants in the Locale class are country names, not country codes. For instance, there is Locale.ITALY , but no Locale.IT . That is why there is also no Locale.GB .

The codes you mean are ISO 3166 alpha-2 country codes. They are used as a parameter in Locale constructor only. It is up to you how you name this object afterwards.

The other answers are correct.

java.time

Here's the current version of your code:

        LocalDate today = LocalDate.now(ZoneId.of("Europe/London"));
        int year = today.getYear();
        DayOfWeek weekday = today.getDayOfWeek();

        System.out.println(year);
        System.out.println(weekday);

The output today (17th June) is

2018
SUNDAY

You don't even need a locale this far (but time zone is crucial, and you may also want to format your date for a specific locale, of course).

The reason we passed a locale to GregorianCalendar (in the old days when we used that class) was it determined the week scheme: the first day of the week is Saturday, Sunday or Monday depending on the locale. Today if you need a week scheme for a particular locale, you should use the WeekFields class and its static of​(Locale) method.

The GregorianCalendar class is long outdated. It's also poorly designed, so I recommend you don't use it.

In the below function i am trying to set the TimeZone "Asia/Singapore" to the date String sent in "yyy-MM-dd" format, you can pass even dateString format as one more parameter and set that too SimpleDateFormat object, this is the perfect solution how to localize time in GregorianCalendar.

public static GregorianCalendar getGregorianCalendarFromDateString(String dateString) {
    GregorianCalendar gregCal = null;
    try {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Singapore"));
        Date date = formatter.parse(dateString);
        gregCal = new GregorianCalendar();
        gregCal.setTime(date);
        gregCal.setTimeZone(TimeZone.getTimeZone("Asia/Singapore"));
    } catch (DatatypeConfigurationException | ParseException e) {
        LOG.error("getGregorianCalendarFromDateString::"+
                "Caught error while parsing date::"+e.getMessage());
    }
    return gregCal;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM