mysql dateofweek_java和mysql的week base year格式化问题

通过java.time.temporal.WeekFields#of(java.time.DayOfWeek, int)方法可以方便地获得自定义规则的TemporalField实例。

两个参数:

DayOfWeek firstDayOfWeek 指定一周从星期几开始

int minimalDaysInFirstWeek 指定一年中的第一周至少需要的天数

调用WeekFields.of(DayOfWeek.SUNDAY, 7)即可得到对应mysql5.7中'%X-%V'格式的规则

final WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7);

final DateTimeFormatter formatter = new DateTimeFormatterBuilder()

.parseCaseInsensitive()

.appendValue(weekFields.weekBasedYear(), 4, 10, SignStyle.EXCEEDS_PAD)

.appendLiteral("-")

.appendValue(weekFields.weekOfWeekBasedYear(), 2).toFormatter();

Stream.iterate(LocalDate.parse("2017-12-29"), date -> date.plusDays(1)).limit(10).forEach(date -> {

System.out.printf("%-12s%-15s%10s\n",date, date.getDayOfWeek(), formatter.format(date));

}

);

输出

2017-12-29 FRIDAY 2017-52

2017-12-30 SATURDAY 2017-52

2017-12-31 SUNDAY 2017-53

2018-01-01 MONDAY 2017-53

2018-01-02 TUESDAY 2017-53

2018-01-03 WEDNESDAY 2017-53

2018-01-04 THURSDAY 2017-53

2018-01-05 FRIDAY 2017-53

2018-01-06 SATURDAY 2017-53

2018-01-07 SUNDAY 2018-01

mysql 5.7对照

select date_format('2017-12-29', '%X-%V'),

date_format('2017-12-30', '%X-%V'), # 周六

date_format('2017-12-31', '%X-%V'),

date_format('2018-01-01', '%X-%V'), # 周一

date_format('2018-01-02', '%X-%V'),

date_format('2018-01-03', '%X-%V'),

date_format('2018-01-04', '%X-%V'),

date_format('2018-01-05', '%X-%V'),

date_format('2018-01-06', '%X-%V'), # 周六

date_format('2018-01-07', '%X-%V')

输出

2017-52 2017-52 2017-53 2017-53 2017-53 2017-53 2017-53 2017-53 2017-53 2018-01

tips

WeekFields定义了两个常量实例:

public static final WeekFields ISO = new WeekFields(DayOfWeek.MONDAY, 4) 由ISO-8601定义的规则,一周从星期一开始,第一周至少需要4天

public static final WeekFields SUNDAY_START = WeekFields.of(DayOfWeek.SUNDAY, 1)欧洲国家和美国使用的规则。(This week definition is in use in the US and other European countries) 一周从星期天开始,第一周至少需要1天

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值