拆分期间分为java部分

时间:2014-07-13 16:18:49

标签: java timestamp

我有以下日期时间:

Start: 2013-1-1  00:00:00.000
End:   2013-1-31 23:59:59.599

我如何在每个时期的分段中将其分成int段。

例如,如果天数= 1:

2013-1-1  00:00:00.000-> 01-01-2013 23:59:59
2013-1-2  00:00:00.000 -> 2013-1-2  23:59:59
2013-1-3  00:00:00.000 -> 2013-1-3  23:59:59 
etc...

我已经为c#阅读了这篇文章Split period into month parts,我试着这样做:

private static ArrayList<Period>  splitPeriods(int days){
        ArrayList<Period> list = new ArrayList<>();
        java.sql.Timestamp start = new Timestamp(110, 0, 1, 00, 00, 00, 00);
        java.sql.Timestamp end = new Timestamp(  110, 2, 1, 00, 00, 00, 00);
        Timestamp current = start;
        Period period;
        while(current.before(end)){
            period = new Period();
            period.setStart(current);
            current.setDate((current.getDate()+days));    
            period.setEnd(current);

            System.out.println(period.getStart() +"\t"+period.getEnd());

        }
        return list;
    }

课程时间:

public class Period {

    private java.sql.Timestamp end;
    private java.sql.Timestamp start;

//..set - get
}

但是期间的输出是:

2013-1-2  00:00:00.000 -> 2013-1-2  00:00:00.000
2013-1-3  00:00:00.000 -> 2013-1-3  00:00:00.000
etc...

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我找到了解决方案。 Ι为开始时段和结束时段创建新的时间戳。 开始周期采用递增前的当前时间戳的值。 结束时段采用递增后的当前时间戳的值。

代码:

期间等级

public class Period {

    private java.sql.Timestamp end;
    private java.sql.Timestamp start;

//..set - get
}



主要课程

public class MainClass{


    public static void main(String[] args) {
        ArrayList<Period> list = splitPeriods(15); 

    }

private static ArrayList<Period>  splitPeriods(int days){
        ArrayList<Period> list = new ArrayList<>();
        java.sql.Timestamp start = new Timestamp(110, 2, 1, 00, 00, 00, 00); //start 1 March 2010
        java.sql.Timestamp end = new Timestamp(  110, 3, 30, 00, 00, 00, 00); //end 30 April 2010
        Timestamp current = start;
        Period period;
        while(current.before(end)){
            period = new Period();
            //new timestamp for start Period
            java.sql.Timestamp startPeriod = new Timestamp(current.getTime());
            //change current timestamp
            current.setDate((current.getDate()+days));    

            //new timestamp for end period.
            //set prev day and time 23:59:59...
            java.sql.Timestamp endPeriod = new Timestamp(current.getTime());
            endPeriod.setDate(endPeriod.getDate()-1);
            endPeriod.setHours(23);
            endPeriod.setMinutes(59);
            endPeriod.setSeconds(59);
            endPeriod.setNanos(599999999);

            //add to list
            period.setStart(startPeriod);
            period.setEnd(endPeriod);

            System.out.println(period.getStart() +"\t"+period.getEnd());

        }
        return list;
    }
}



结果:

input
start: 1 March 2010
end: 30 April 2010


output
2010-03-01 00:00:00.0   2010-03-15 23:59:59.599999999
2010-03-16 00:00:00.0   2010-03-30 23:59:59.599999999
2010-03-31 00:00:00.0   2010-04-14 23:59:59.599999999
2010-04-15 00:00:00.0   2010-04-29 23:59:59.599999999

答案 1 :(得分:0)

这是一个棘手的正则表达式问题!我认为这种困难应被视为正则表达可能不是最佳工具的证据。

激活multiline flag后,此正则表达式将起作用:

  • 找到:Start: ([0-9]{4}(?:-[0-9]{1,2}){2} +(?:[0-9]{2}:){2}[0-9]{2})(?:\.[0-9]{2,3})?\nEnd: ([0-9]{4}(?:-[0-9]{1,2}){2} +(?:[0-9]{2}:){2}[0-9]{2})(?:\.[0-9]{2,3})?\n
  • 替换为:$1 -> $2
  • 试一试:http://regex101.com/r/lQ0wR9/1

自由间隔:

  • Start: ([0-9]{4}Start,(开始捕获组),4位数字
  • (?: - [0-9] {1,2}){2} +`:短划线后跟两位数(两次
  • (?:[0-9]{2}:){2}[0-9]{2}):两位数字然后冒号(两次),后跟两位数字
  • (?:\.[0-9]{2,3})?:可选:点后跟两位或三位
  • \r\n:Newline
  • End: ...End后跟与Start
  • 之后相同的正则表达式

输入:

Start: 2013-1-1  00:00:00.000
End:   2013-1-31 23:59:59.59
Start: 2013-1-1  00:00:00.000
End:   2013-1-1  23:59:59
Start: 2013-1-2  00:00:00.000
End:   2013-1-2  23:59:59
Start: 2013-1-3  00:00:00.000
End:   2013-1-3  23:59:59

爪哇:

   import  java.util.regex.Matcher;
   import  java.util.regex.Pattern;
public class StartEndDateLinesToArrowSingleLine  {
   private static final String LINE_SEP = System.getProperty("line.separator", "\r\n");
   public static final void main(String[] ignored)  {

      String regexMain = "([0-9]{4}(?:-[0-9]{1,2}){2} +" +
         "(?:[0-9]{2}:){2}[0-9]{2})" +
         "(?:\\.[0-9]{2,3})?" + LINE_SEP;
      String findWhatRegex = "Start: " + regexMain + "End:   " + regexMain;
      String rplcWith = "$1 -> $2";

      String input = new StringBuilder().
         append("Start: 2013-1-1  00:00:00.000").append(LINE_SEP).
         append("End:   2013-1-31 23:59:59.599").append(LINE_SEP).
         append("Start: 2013-1-1  00:00:00.000").append(LINE_SEP).
         append("End:   2013-1-1  23:59:59"    ).append(LINE_SEP).
         append("Start: 2013-1-2  00:00:00.000").append(LINE_SEP).
         append("End:   2013-1-2  23:59:59"    ).append(LINE_SEP).
         append("Start: 2013-1-3  00:00:00.000").append(LINE_SEP).
         append("End:   2013-1-3  23:59:59"    ).append(LINE_SEP).toString();

      Matcher mtchr = Pattern.compile(findWhatRegex, Pattern.MULTILINE).matcher(input);

      StringBuffer bfr = new StringBuffer();
      while(mtchr.find())  {
         bfr.setLength(0);
         mtchr.appendReplacement(bfr, rplcWith);
         bfr.append(LINE_SEP);
         mtchr.appendTail(bfr);
         mtchr.reset(bfr.toString());
      }
      System.out.println(bfr.toString());
   }
}

输出:

2013-1-1  00:00:00 -> 2013-1-31 23:59:59
2013-1-1  00:00:00 -> 2013-1-1  23:59:59
2013-1-2  00:00:00 -> 2013-1-2  23:59:59
2013-1-3  00:00:00 -> 2013-1-3  23:59:59

答案 2 :(得分:0)

private List<DaySlot> getDaySlots(Date start, Date end) {
    List<DaySlot> ret = new ArrayList<>();
    Date current = start;
    while(current.before(end)) {
        Date startOfTheDay = getStartOfTheDay(current);
        Date endOfTheDay = getEndOfTheDay(startOfTheDay);
        ret.add(new DaySlot(current, endOfTheDay.before(end)? endOfTheDay: end));
        current = getNextDay(startOfTheDay);
    }
    return ret;
}



private LocalDate getDateOnly(Date date) {
    return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

private Date getStartOfTheDay(Date current) {
    return Date.from(getDateOnly(current).atStartOfDay(ZoneId.systemDefault()).toInstant());
}

private Date getNextDay(Date startOftheDay) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startOftheDay);
    calendar.add(Calendar.DATE, 1);
    System.out.println(calendar.getTime());
    return calendar.getTime();
}

private Date getEndOfTheDay(Date startOftheDay) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startOftheDay);
    calendar.add(Calendar.HOUR_OF_DAY, 23);
    calendar.add(Calendar.MINUTE, 59);
    calendar.add(Calendar.SECOND, 59);
    calendar.add(Calendar.MILLISECOND, 599);
    System.out.println(calendar.getTime());
    return calendar.getTime();
}

电话如下:

    List<DaySlot> daySlots = getDaySlots(durationStartTime, durationEndTime);