如何获取本地化的短星期名称(Mo/Tu/We/Th...)

发布于 2024-09-25 00:24:28 字数 57 浏览 3 评论 0原文

我可以在 Java 中获得本地化的短星期名称(英语为 Mo/Tu/We/Th/Fr/Sa/Su)吗?

Can I get localized short day-in-week name (Mo/Tu/We/Th/Fr/Sa/Su for English) in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心是晴朗的。 2024-10-02 00:24:28

最好的方法是使用 java.text.DateFormatSymbols

DateFormatSymbols symbols = new DateFormatSymbols(new Locale("it"));
// for the current Locale :
//   DateFormatSymbols symbols = new DateFormatSymbols(); 
String[] dayNames = symbols.getShortWeekdays();
for (String s : dayNames) { 
   System.out.print(s + " ");
}
// output :  dom lun mar mer gio ven sab 

The best way is with java.text.DateFormatSymbols

DateFormatSymbols symbols = new DateFormatSymbols(new Locale("it"));
// for the current Locale :
//   DateFormatSymbols symbols = new DateFormatSymbols(); 
String[] dayNames = symbols.getShortWeekdays();
for (String s : dayNames) { 
   System.out.print(s + " ");
}
// output :  dom lun mar mer gio ven sab 
予囚 2024-10-02 00:24:28

如果标准缩写适合您,只需使用 Calendar 类,如下所示:

myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);

If standard abbreviations are fine with you, just use Calendar class like this:

myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
浊酒尽余欢 2024-10-02 00:24:28

使用 SimpleDateFormat 的示例:

Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
String asWeek = dateFormat.format(now);

SimpleDateFormat 比 C 风格的 String.format 和 System.out.printf 存在的时间更长,我认为您会发现大多数 Java 开发人员会更熟悉它并且在现有代码库中使用得更多,所以我推荐这种方法。

An example using SimpleDateFormat:

Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
String asWeek = dateFormat.format(now);

SimpleDateFormat as been around longer than the C-style String.format and System.out.printf, and I think you'd find most Java developers would be more familiar with it and more in use in existing codebases, so I'd recommend that approach.

美胚控场 2024-10-02 00:24:28

java.time

针对使用 Java 8 及更高版本的用户的更新。

ZoneId zoneId = ZoneId.of("America/Los_Angeles");

Instant instant = Instant.now();

ZonedDateTime zDateTime = instant.atZone(zoneId);

DayOfWeek day = zDateTime.getDayOfWeek();

显示输出。

System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.US));
System.out.println(day.getDisplayName(TextStyle.NARROW, Locale.US));

运行时。查看在 IdeOne.com 上实时运行的类似代码

周二

T

java.time

Update for those using Java 8 and later.

ZoneId zoneId = ZoneId.of("America/Los_Angeles");

Instant instant = Instant.now();

ZonedDateTime zDateTime = instant.atZone(zoneId);

DayOfWeek day = zDateTime.getDayOfWeek();

Show output.

System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.US));
System.out.println(day.getDisplayName(TextStyle.NARROW, Locale.US));

When run. See similar code run live at IdeOne.com.

Tue

T

不可一世的女人 2024-10-02 00:24:28

DateTimeFormatter#localizedBy

Java SE 10 开始,您可以使用 DateTimeFormatter#localizedBy

演示:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {

    public static void main(String args[]) {
        DateTimeFormatter dtfHindi = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("hi"));
        DateTimeFormatter dtfBangla = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("bn"));
        DateTimeFormatter dtfGerman = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("de"));
        DateTimeFormatter dtfEnglish = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("en"));

        // Replace ZoneId.systemDefault() with the applicable timezone e.g.
        // ZoneId.of("Asia/Calcutta")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        System.out.println(dtfHindi.format(today));
        System.out.println(dtfBangla.format(today));
        System.out.println(dtfGerman.format(today));
        System.out.println(dtfEnglish.format(today));
    }
}

输出:

शुक्र
শুক্র
Fr.
Fri

或者,从 Java SE 8 开始,您可以使用 DayOfWeek#getDisplayName 与适用的区域设置

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {

    public static void main(String args[]) {
        // Replace ZoneId.systemDefault() with the applicable timezone e.g.
        // ZoneId.of("Asia/Calcutta")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("hi")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("bn")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("de")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("en")));
    }
}

输出:

शुक्र
শুক্র
Fr.
Fri

DateTimeFormatter#localizedBy

Starting with Java SE 10, you can use DateTimeFormatter#localizedBy.

Demo:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {

    public static void main(String args[]) {
        DateTimeFormatter dtfHindi = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("hi"));
        DateTimeFormatter dtfBangla = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("bn"));
        DateTimeFormatter dtfGerman = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("de"));
        DateTimeFormatter dtfEnglish = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("en"));

        // Replace ZoneId.systemDefault() with the applicable timezone e.g.
        // ZoneId.of("Asia/Calcutta")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        System.out.println(dtfHindi.format(today));
        System.out.println(dtfBangla.format(today));
        System.out.println(dtfGerman.format(today));
        System.out.println(dtfEnglish.format(today));
    }
}

Output:

शुक्र
শুক্র
Fr.
Fri

Alternatively, starting with Java SE 8, you can use DayOfWeek#getDisplayName with the applicable Locale.

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {

    public static void main(String args[]) {
        // Replace ZoneId.systemDefault() with the applicable timezone e.g.
        // ZoneId.of("Asia/Calcutta")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("hi")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("bn")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("de")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("en")));
    }
}

Output:

शुक्र
শুক্র
Fr.
Fri
独孤求败 2024-10-02 00:24:28

您不能使用 Calendar 类来执行此操作(除非您自己编写),但可以使用 Date 类来执行此操作。 (两者通常同时使用)。

这是一个例子:

import java.util.Date;

public class DateFormatExample {

  public static void main(String[] args) {
    Calendar nowCal = Calendar.getInstance(); // a Calendar date
    Date now = new Date(nowCal.getTimeInMillis()); // convert to Date
    System.out.printf("localized month name: %tB/%TB\n", now, now); 
    System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now); 
    System.out.printf("localized day name: %tA/%TA\n", now, now);
    System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now); 
  }
}

输出:

localized month name: June/JUNE 
localized, abbreviated month: Jun/JUN 
localized day name: Friday/FRIDAY 
localized, abbreviated day: Fri/FRI

You can't do it with the Calendar class (unless you write your own), but you can with the Date class. (The two are usually used hand-in-hand).

Here's an example:

import java.util.Date;

public class DateFormatExample {

  public static void main(String[] args) {
    Calendar nowCal = Calendar.getInstance(); // a Calendar date
    Date now = new Date(nowCal.getTimeInMillis()); // convert to Date
    System.out.printf("localized month name: %tB/%TB\n", now, now); 
    System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now); 
    System.out.printf("localized day name: %tA/%TA\n", now, now);
    System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now); 
  }
}

Output:

localized month name: June/JUNE 
localized, abbreviated month: Jun/JUN 
localized day name: Friday/FRIDAY 
localized, abbreviated day: Fri/FRI
岁吢 2024-10-02 00:24:28

您可以使用它:

public String getDayString(){
    Locale locale = Locale.getDefault();
    LocalDate date = LocalDate.now();
    DayOfWeek day = date.getDayOfWeek();
    return day.getDisplayName(TextStyle.FULL, locale);
}

结果将是:星期一

You can use this :

public String getDayString(){
    Locale locale = Locale.getDefault();
    LocalDate date = LocalDate.now();
    DayOfWeek day = date.getDayOfWeek();
    return day.getDisplayName(TextStyle.FULL, locale);
}

The result will be: Monday

夏日落 2024-10-02 00:24:28
//you need to use joda library           
List<String> dayList = new ArrayList<String>();
String[] days = new String[7];
int a=2; 
//a =starting day of week 1=sunday ,2=monday
Calendar c2 = Calendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
c2.set((Calendar.DAY_OF_WEEK),a); 
int maxDay = c2.getActualMaximum(Calendar.DAY_OF_WEEK);
for(int i=0;i<maxDay;i++)
{                 
   days[i] = df.format(c2.getTime());
   c2.add(Calendar.DAY_OF_MONTH, 1);
   String dayOfWeek = new LocalDate( days[i]).dayOfWeek().getAsShortText();
   dayList.add(dayOfWeek);         
}            
for(int i=0;i<maxDay;i++)
{
    System.out.print(" '"+dayList.get(i)+"'");
}            
//you need to use joda library           
List<String> dayList = new ArrayList<String>();
String[] days = new String[7];
int a=2; 
//a =starting day of week 1=sunday ,2=monday
Calendar c2 = Calendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
c2.set((Calendar.DAY_OF_WEEK),a); 
int maxDay = c2.getActualMaximum(Calendar.DAY_OF_WEEK);
for(int i=0;i<maxDay;i++)
{                 
   days[i] = df.format(c2.getTime());
   c2.add(Calendar.DAY_OF_MONTH, 1);
   String dayOfWeek = new LocalDate( days[i]).dayOfWeek().getAsShortText();
   dayList.add(dayOfWeek);         
}            
for(int i=0;i<maxDay;i++)
{
    System.out.print(" '"+dayList.get(i)+"'");
}            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文