java SimpleDateformat 和“月中的星期几”(F)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14231978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SimpleDateformat and "Day of week in month" (F)
提问by Francesco
I would like to get which day of the week is the current day and looking the SimpleDateFormat class I tought that the "F" is what I need. So I wrote a little test:
我想知道一周中的哪一天是当天,并查看 SimpleDateFormat 类,我认为“F”是我需要的。所以我写了一个小测试:
System.out.println(new SimpleDateFormat("F").format(new Date()));
Today is wednesday and I expect to get 3 as output. Instead I get 2.
今天是星期三,我希望得到 3 作为输出。相反,我得到 2。
As english isn't my mothertongue, did I missunderstand the meaning of the format?
由于英语不是我的母语,我是否误解了格式的含义?
回答by Evgeniy Dorofeev
F
- Day of week in month
F
- 一个月中的星期几
E
- Day name in week
E
- 周中的日期名称
try u
- Day number of week (1 = Monday, ..., 7 = Sunday)
try u
- 星期几(1 = 星期一,...,7 = 星期日)
Note that 'u' is since Java 7, but if you need just day number of the week then use Calendar
请注意,'u' 是从 Java 7 开始的,但如果您只需要一周中的天数,则使用 Calendar
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
You can change first day of week by changing Locale or directly as
您可以通过更改区域设置或直接作为
c.setFirstDayOfWeek(Calendar.SUNDAY);
回答by kan
Today is the second Wednesday in the current month.
今天是当月的第二个星期三。
回答by Basil Bourque
The java.util.Calendar/.Date and related classes are a confusing mess as you have learned the hard way. Counting from zero for month numbers and day-of-week numbers is one of many poor design choices made in those old classes.
java.util.Calendar/.Date 和相关的类是一个混乱的混乱,因为你已经学会了艰难的方式。从零开始计算月数和星期数是这些旧类中做出的许多糟糕的设计选择之一。
java.time
时间
Those old classes have been supplanted in Java 8 and later by the java.timeframework. The new classes are inspired by the highly successful Joda-Timeframework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extraproject. See the Tutorial.
那些旧的类已在 Java 8 和更高版本中被java.time框架取代。新类的灵感来自非常成功的Joda-Time框架,作为其继承者,概念相似但重新构建。由JSR 310定义。由ThreeTen-Extra项目扩展。请参阅教程。
DayOfWeek
DayOfWeek
For day of week, use the well-named DayOfWeek
enum.
If you want an integer for day-of-week compliant with ISO 8601where Monday = 1 and Sunday = 7, you can extract that from an instance of DayOfWeek
.
如果您想要一个符合ISO 8601的星期几的整数,其中星期一 = 1 和星期日 = 7,您可以从DayOfWeek
.
Conversion
转换
If starting with a java.util.Calendar object, convert to java.time.
如果从 java.util.Calendar 对象开始,则转换为 java.time。
An Instant
is a moment on the timeline in UTC.
AnInstant
是UTC时间线上的一个时刻。
Instant instant = myJavaUtilCalendarObject.toInstant();
Apply a time zone in order to get a date in order to get a day-of-week.
应用时区以获得日期以获得星期几。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
DayOfWeek dayOfWeek = zdt.getDayOfWeek();
Note that time zone in crucial in determining a date, and therefore a day-of-week. The date is not the same around the world simultaneously. A new day dawns earlier in Paris than in Montréal, for example.
请注意,时区对于确定日期至关重要,因此是一周中的某一天。世界各地的日期不一样。例如,巴黎比蒙特利尔更早地迎来新的一天。
Number Of Day-Of-Week
星期几
Now that we have java.time and this enum built into Java, I suggest you freely pass around those enum instances rather than a magic number.
既然我们已经有了 java.time 并且这个 enum 内置在 Java 中,我建议你自由地传递这些 enum 实例而不是一个幻数。
But if you insist on an integer, ask the DayOfWeek
.
但是,如果您坚持使用整数,请询问DayOfWeek
.
int dayOfWeekNumber = dayOfWeek.getValue();
String Of Day-Of-Week
一串星期几
That enum generates a localized String for the name of the day-of-week.
该枚举为星期几的名称生成一个本地化的字符串。
String output = dayOfWeek.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ); // Or Locale.ENGLISH.
回答by Juvanis
Indexes for the days of week start from 0, not 1.
星期几的索引从 0 开始,而不是 1。
回答by Subhrajyoti Majumder
F -> Day of week in month(1-5)
Today is - 09/01/2013(dd/MM/yyyy)
which fall 2nd in week so it has printed 2.
今天是 -09/01/2013(dd/MM/yyyy)
本周第 2 次,所以它打印了 2。
If you try with 16/01/2013
then it would print 3.
如果您尝试使用16/01/2013
,它将打印 3。
回答by vitralyoz
just use this method for this.
只需为此使用此方法。
public String day(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEE", new Locale("tr", "TR"));
return sdf.format(date);
}
回答by Zaw Htet Aung
F = Day of Week in Month
1dayto 7day, it will print 1.
8dayto 14day, it will print 2.
15dayto 21day, it will print 3.
22dayto 28day, it will print 4and
29dayto 31day, it will print 5.
F =星期几月份
1天到7天,将打印1。
8天到14天,将打印2。
15 天到21 天,它将打印3.
22 天到28 天,它会打印4和
29 天到31 天,它会打印5。