java - 在 Java 中生成两个日期之间的日开始时间和日结束时间。夏令时问题

标签 java date datetime calendar

我必须生成两个日期之间的天数。例如:

1420090495000 -> 2015 年 1 月 1 日

1430458495000 -> 2015 年 5 月 1 日

我必须生成所有日期的时间戳,例如

2015年1月1日00:00:00 - 2015年1月1日23:59:59

2015年1月2日00:00:00 - 2015年1月2日23:59:59

2015年1月3日00:00:00 - 2015年1月3日23:59:59

等等

我有能力做到这一点。但我在夏令时问题上遇到了一些问题。在行进的某个地方,它会像这样生成

2015年3月8日 00:00:00 - 2015年3月9日 00:01:00 这是错误的,应该像2015年3月8日 00:00:00 - 2015年3月8日23:59:59

我发现它是因为夏令时问题。如何解决这个问题?

我的代码是:

public static List<String> getDatesRange(long start, long end, String tzOffset) {
//tzOffset is 420. for USA

        TimeZone tz = TimeZone.getTimeZone(tzOffset);

        List<String> dates=new ArrayList();
        Calendar calendar = Calendar.getInstance(tz);

        calendar.set(Calendar.DAY_OF_WEEK, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);


        while (start<end) {
            calendar.setTimeInMillis(start);
            long startTime = calendar.getTimeInMillis();
            int year= calendar.getWeekYear();


            long endTime = start + (1 * 24 * 3600 * 1000L);

            if(endTime<end) {
                endTime-=1000;
                System.out.println("Start Date= " + new Date(new Timestamp(start).getTime())+" ("+startTime+"), End Date= "+new Date(new Timestamp(endTime).getTime())+"("+endTime+")");

dates.add(startTime+"-"+endTime);
                start= endTime+1000;
            }
            else{
                System.out.println("Start Date= " + new Date(new Timestamp(start).getTime()) + " (" + startTime + "), End Date= " + new Date(new Timestamp(end).getTime()) + "(" + end + ")");
                start=end;
                dates.add(startTime+"-"+end);
            }
        }
        return dates;
    }

最佳答案

对此我有一些疑问:

  • 为什么使用长整型来定义日期?
  • 为什么返回字符串列表而不是日期列表?
  • 在这种情况下为什么要考虑时区?

尽管如此,您只需使用日历和 SimpleDateFormat 即可实现此目的,像这样:

public static Calendar getDayStart(final long timeInMillis) {
    final Calendar cal = Calendar.getInstance();
    // end time as a date
    cal.setTimeInMillis(timeInMillis);

    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    return cal;
}

public static List<String> getDatesRange(final long start, final long end) {

    final Calendar cal = getDayStart(start);
    final Date startDate = cal.getTime();

    final Calendar calEnd = getDayStart(end);
    //adding one day because of the strict comparison in the while below
    calEnd.add(Calendar.DAY_OF_YEAR, 1);
    final Date endDate = calEnd.getTime();

    final SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");

    final List<String> dates = new ArrayList<String>();
    final Date dayEnd;
    String currentDay = "";

    while(cal.getTime().before(endDate)) {
        currentDay = formatter.format(cal.getTime());
        currentDay += " - ";
        //going to the end of the day
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.add(Calendar.SECOND, -1);
        currentDay += formatter.format(cal.getTime());
        //going to next day again and continue the loop
        cal.add(Calendar.SECOND, 1);
        //add what we computed to the list of days
        dates.add(currentDay);
    }

    return dates;
}

关于java - 在 Java 中生成两个日期之间的日开始时间和日结束时间。夏令时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635795/

相关文章:

带时区的 Mysql 日期时间

sql-server - SQL Server select 语句用于指定日期范围?

python - 创建包含月份中的天数的列表

java - 为什么 Weblogic 工作管理器拒绝工作

java - 如何制作一个记录器,可以从不同的包记录到不同的文件

Java Salesforce 集成错误

Python 2.7 - 使用 +01 :00 转换日期时间

php - 如何区分 mysql 时间戳和 php 当前时间?

java - 如何验证 List 类型的@RequestParam 的大小

java - 使用带正则表达式的范围验证 4 位数年份