java8+第几周,Java 8:从纪元时间开始获取周数

Given LocalDate I want to convert to week number since Epoch

One way to do that is:

LocalDate date = datePicker.getValue(); // input from your date picker

Locale locale = Locale.US;

int weekOfYear = date.get(WeekFields.of(locale).weekOfWeekBasedYear());

And X = find weeks since Epoch to prevYear

And then result = X + weekOfYear

Although someway we can find out "week number since epoch", is there clean solution to find it using Java 8 ?

UPDATE:

Even above solution wont work as one week(always starting from Sunday) can span across two years

解决方案

I am turning the comment by Tunaki into an Answer.

tl;dr

ChronoUnit.WEEKS.between (

LocalDate.ofEpochDay ( 0 ) ,

LocalDate.now( ZoneOffset.UTC )

)

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Here we use UTC as the time zone for today’s date, in accordance with the epoch being defined in UTC.

LocalDate today = LocalDate.now( ZoneOffset.UTC );

ChronoUnit

The ChronoUnit class has methods for calculating an elapsed number of years or months or weeks or such. For weeks it simply takes the number of days and divides by 7. So the first day-of-week is irrelevant. Not sure if that meets your needs or not as the Question is vague.

LocalDate today = LocalDate.now ( ZoneOffset.UTC ); // Using UTC to match the definition of the Java epoch.

LocalDate epoch = LocalDate.ofEpochDay ( 0 );

long weeks = ChronoUnit.WEEKS.between ( epoch , today );

Dump to console.

System.out.println ( "epoch: " + epoch + " | today in UTC: " + today + " | weeks: " + weeks );

epoch: 1970-01-01 | today in UTC: 2016-08-29 | weeks: 2434

You could adjust your starting date to a specific day-of-week if that makes sense for your needs. I'm not sure if that fits because the Question is not clear.

LocalDate epoch = LocalDate.ofEpochDay ( 0 );

LocalDate start = epoch.with ( TemporalAdjusters.nextOrSame ( DayOfWeek.SUNDAY ) ); // Get the first Sunday that *is* the epoch or *follows* the epoch.

long weeks = ChronoUnit.WEEKS.between ( start , today );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Java SE 8, Java SE 9, and later

Built-in.

Part of the standard Java API with a bundled implementation.

Java 9 adds some minor features and fixes.

Java SE 6 and Java SE 7

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.

Android

Later versions of Android bundle implementations of the java.time classes.

For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值