37

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

4

7 回答 7

64

最好的方法是使用 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 
于 2010-09-24T21:51:09.320 回答
38

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

myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
于 2011-07-29T16:41:38.580 回答
27

使用 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 开发人员会更熟悉它并且更多地用于现有代码库,所以我建议方法。

于 2010-09-24T22:19:31.780 回答
10

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 上实时运行的类似代码。

周二

于 2016-10-17T16:27:44.793 回答
4

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适用的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")));
    }
}

输出:

शुक्र
শুক্র
Fr.
Fri
于 2021-03-19T18:21:16.497 回答
3

你不能用 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
于 2010-09-24T21:06:24.763 回答
0
//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)+"'");
}            
于 2014-12-24T05:48:16.420 回答