How to use Java Time parses the date in the string from the year and week

In old Java, I could do this:

System.out.println(new SimpleDateFormat("yyyy w",Locale.UK).parse("2015 1"));
// shows Mon Dec 29 00:00:00 CET 2014

System.out.println(new SimpleDateFormat("yyyy w",Locale.US).parse("2015 1"));
// shows Mon Dec 28 00:00:00 CET 2014

I want to use Java in Java 8 time.

System.out.println( LocalDate.parse("2015 1",DateTimeFormatter.ofPattern("yyyy w",Locale.US)));

result:

java. time. format. Datetimeparseexception: cannot parse text '2015 1': cannot get localdate from temporalaccessor: {weekofweekbasedyear [weekfields [Sunday, 1]] = 1, year = 2015}, ISO type is Java time. format. Parsed

How to use Java Do this in time?

In addition, I am not satisfied. I have to determine the first day of the week through locale: Monday and Sunday It is not a village function, but a calendar function I want to use something like Java time. temporal. WeekFields. Something like ISO to show the world from Monday

I found similar cases: https://stackoverflow.com/questions/3941700/how-to-get-dates-of-a-week-i-know-week-number

But it does not apply to Java in Java 8 time. In addition, the solution of creating a date object first and setting the correct week later is not elegant I want to create a final date at once

Solution

Direct answers and solutions:

System.out.println( 
  LocalDate.parse("2015 1",new DateTimeFormatterBuilder().appendPattern("YYYY w")
    .parseDefaulting(WeekFields.ISO.dayOfWeek(),1)
    .toFormatter()));
// output: 2014-12-29

explain:

a) You should use y instead of y because you are interested in ISO - 8601 week dates, not in the year

b) Calendar dates cannot be formed only by giving (week based) years and weeks The day of the week is important to determine the date of the specified calendar week The predefined formatter for week dates needs to be missing the day of the week So you need to use Builder - pattern to build a special parser Then it is necessary to tell the parser the day of the week through the method parsedefaulting ()

c) I insist (and defend jsr-310 here) that the problem at the beginning of the week is not a calendar problem, but a country dependent problem The United States and France (for example) use the same calendar, but have different views on how to define a week You can use the explicit ISO reference field weekfields ISO. Dayofweek() to apply the ISO - 8601 standard Note: the test shows the use of chronofield DAY_ OF_ Week and locale Root does not always seem to guarantee the behavior of ISO week, as shown in my first version of the answer (the reason is not clear - my close observation seems necessary to inspire non intuitive behavior)

d) Java time package does a good job – except Monday is specified as the number 1 I would prefer enumeration Or use enum and its method getvalue()

e) Side notice: simpledateformat is loose by default The java time package is more stringent and refuses to use air to create a missing day of the week - even in loose mode (which is a good thing in my opinion) Software should not guess like this. On the contrary, programmers should think more about which day is right Also here: the application requirements in the United States and France may be different about the correct default settings

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>