- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须生成两个日期之间的天数。例如:
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/
我正在使用 C#。 我知道我可以用 ToLongDateString() 显示如下内容: Friday, February 27, 2009 我喜欢做的是展示类似的东西: Febru
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
我需要检查是否 DateTime.Now是在每个月的前 3 个工作日(周一至周五)。我还需要提供一个 List有国定假日,应相应处理。 如果 DateTime.Now 是星期六并且是该月的 1 号,则
我想以 2012 年 1 月 1 日 - 2012 年 1 月 31 日的格式显示日期 并获取由 dateranges['jan 1,2012 - jan 31,2012','December 1,2
我有这两个函数可以以正确的格式 (mm-dd-yyyy) 创建一个新字符串,但现在它似乎工作得不太好......当我输入日期 31-03-2013 这是一个有效的日期,它出现在 04-01-2013
我只想在 Fullcalendar 中加载 December 月份,其中 2 个日期启用 2012 年 12 月 14 日、15 日。 var date = new Date(2012, 11); v
我创建了一个convertDate类,用于转换我的时间戳,即Date().sinceReferenceDate(即自2001年1月1日凌晨12:00起)。我有一个问题:当时间戳的日期超过 3 周前时,
我有来自两个来源的数据,我试图在日期/时间上进行匹配。问题是我没有得到匹配。所以我将日期/时间转换为 Julian 并遇到了一些奇怪的事情。 8/11/2015 6:50:22.000 42227
我正在尝试将毫秒转换为如下所示的日期:Oct 04, 2013 .我将毫秒转换为日期对象: var d1 = new Date(milliseconds); 然后输出如下内容: Fri Oct 04
我正在尝试使用 javascript 进行倒计时。但是,我的倒计时只能计算天数、小时数、分钟数和秒数。我还想显示年份和月份。 以下是我的代码: today = new Date();
我有下面的查询不适合我, 这个函数 from_tz(to_timestamp(START_TIME, 'YYYYMMDDHH24MISS'), substr(UTC_TIME_CODE_OFFSET,
例如,我有一列数据如下: 17.14.11 17.15.10 18.21.06 现在是 2017 年 11 月 14 日,我想将其更改为 DateTime 对象,例如: 2017-11-14 2017
$date1 = "2000-01-01"; $date2 = "2011-03-14"; $diff = abs(strtotime($date2) - strtotime($date1)); $y
我有以下代码来验证给定日期格式的日期: val df = new SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); try { val
我有一个包含两个 DateTime 成员的类。 但是,当我尝试将表示日期的“人类可读”值分配给 DateTime 成员时,它拒绝它并显示消息“字符串未被识别为有效的 DateTime”。 失败的代码行
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题已经有答案了: Adding days to a date in Java [duplicate] (6 个回答) 已关闭 6 年前。 如何在 java 中将给定的日期转换为日历格式。 示例初
这个问题已经有答案了: Illegal pattern character 'T' when parsing a date string to java.util.Date (4 个回答) 已关闭 6
我现在在香港。如何获取美国的年、月、日? 我已经尝试过: Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new
我正在尝试使用 php 和 Mysql 开发一个搜索系统,但是您如何搜索:例如当数据库中的日期格式为 2012-03-26(年/月/日)时的三月份 最佳答案 根据列数据的格式,这样的事情可能会起作用:
我是一名优秀的程序员,十分优秀!