0

是否有任何简单的库或方法来获取某些时期的星期(从哪个日期〜日期)?

示例:有6 weeks(variable)(从 2012 年 7 月 1 日至 2012 年 8 月 11 日开始)。

我想将 6 周时间缩短为2 portions (variable). 所以结果将是

1) 1 July,2012 ~ 21 July, 2012

2) 22 July,2012 ~ 11 Aug, 2012... etc

使用jodatime,我可以很容易地获得某些时期之间的周数。

我所知道的是Start Date and End Date两者都是变量和cutoffweeks amount(例如6周或4周)。

4

2 回答 2

1
final LocalDate start = new LocalDate();
final LocalDate end3 = start.plusWeeks(3)

Its not exactly clear what you want, but Joda-Time makes most things rather easy.

I guess you need something like :

public void doStruff(int cutOff){
  int portion = cutoff/2;
  final LocalDate start = new LocalDate();
  final LocalDate end = start.plusWeeks(portion)
}
于 2012-06-29T09:40:56.397 回答
0

你可以试试这段代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
    public class DateDiff {  
       public static void main(String[] args) {  
         String s1 = "06/01/2012";
         String s2 = "06/24/2012";
          DateDiff dd = new DateDiff();  
          Date then = null, now = null;  
          DateFormat df = DateFormat.getInstance();  
          df.setTimeZone( TimeZone.getDefault() );  

             try {  
                then = df.parse( s1 + " 12:00 PM" );  
                now = df.parse( s2 + " 12:00 PM" );  
             } catch ( ParseException e ) {  
                System.out.println("Couldn't parse date: " + e );  
                System.exit(1);  
             } 
          long diff = dd.getDateDiff( now, then, Calendar.WEEK_OF_YEAR );  
          System.out.println("No of weeks: " + diff );  
       }  

       long getDateDiff( Date d1, Date d2, int calUnit ) {  
          if( d1.after(d2) ) {    // make sure d1 < d2, else swap them  
             Date temp = d1;  
             d1 = d2;  
             d2 = temp;  
          }  
          GregorianCalendar c1 = new GregorianCalendar();  
          c1.setTime(d1);  
          GregorianCalendar c2 = new GregorianCalendar();  
          c2.setTime(d2);  
          for( long i=1; ; i++ ) {           
             c1.add( calUnit, 1 );   // add one day, week, year, etc.  
             if( c1.after(c2) )  
                return i-1;  
          }  
       }  
    }  
于 2012-06-29T09:35:59.560 回答