简体   繁体   English

根据ISO 8601生成周/年的列表

[英]Generate a list of the week/year according ISO 8601

I try to generate a list of days with their week number (defined by ISO 8601 ) accordingly : 我尝试相应地生成带有星期编号 (由ISO 8601定义)的天列表:

mydate='2012-12-25 02:26:55.983'
for (i=1;i<365;i++)
{
  mydateAsDate=new Date().parse('yyyy-MM-dd H:mm:ss.S',mydate)+i;
  println 'Week ' + mydateAsDate.format('w') + ' => ' + mydateAsDate.format('dd.MM.yyyy');
}

This works but I would like to get the year also like this: 这可行,但我也想得到这样的一年:

Week 1-2013

I can't figure out which year information I should take. 我不知道应该拿哪一年的信息。

Any idea? 任何想法?

As Jon Skeet said, I'd recommend using Joda-Time . 正如Jon Skeet所说,我建议您使用Joda-Time

If you do, the following should fix your issues: 如果您这样做,以下应解决您的问题:

mydate= new DateTime(2012,12,25)
yearLater = myDate.plusYears(1)
while(myDate < yearLater){
  println "Week ${myDate.weekOfWeekyear} - ${myDate.year}"
  myDate = myDate.plusDays(1)
}

Not sure I understand, but you mean like: 不确定我是否了解,但您的意思是:

String startDateString = '2012-12-25 02:26:55.983'
Date startDate = Date.parse( 'yyyy-MM-dd H:mm:ss.S', startDateString )
(1..364).each { i ->
    println( (startDate++).format( "dd.MM.yyyy : 'Week' w'-'yyyy" ) )
}

I got it : SimpleDateFormat delivers the right week year information when using the YYYY format 我知道了:使用YYYY格式时,SimpleDateFormat可以提供正确的星期年份信息

thus this is only available in java 1.7 因此这仅在Java 1.7中可用

thanks for your responses though ! 谢谢您的回应!

cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM