Java日期(日历)计算给定日期时间的本地时间中一天的开始 [英] Java Date (Calendar) calculating the start of a day in local time given a date time
问题描述
全部,
在办公室里度过艰难的一天...我想在当地时间(即午夜00)开始一天的训练
在以下几天,我想计算当天午夜的一天的开始时间:
时间开始日(本地)开始日(GMT)
2011-03-27 00:00:00 GMT - > 2011-03-27 00:00:00 GMT - > 2011-03-27 00:00:00 GMT
2011-03-27 01:00:00 GMT - > 2011-03-27 00:00:00 GMT - > 2011-03-27 00:00:00 GMT
2011-03-27 02:00:00 GMT - > 2011-03-27 00:00:00 GMT - > 2011-03-27 00:00:00 GMT
2011-04-01 00:00:00 BST - > 2011-04-01 00:00:00 BST - > 2011-03-31 23:00:00 GMT
2011-10-30 00:00:00 BST - > 2011-10-30 00:00:00 BST - > 2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 BST - > 2011-10-30 00:00:00 BST - > 2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 GMT - > 2011-10-30 00:00:00 BST - > 2011-10-29 23:00:00 GMT
2011-11-01 00:00:00 GMT - > 2011-11-01 00:00:00 GMT - > 2011-11-01 00:00:00 GMT
目前,我将String Time解析为使用SimpleDateFormat的GregorianCalendar。这给我的GMT / UTC时间来计算。
所以我有一些代码将字符串解析为GregorianCalendar:
public GregorianCalendar getCalendar(String dateTime){
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(sdf.parse(dateTime,new ParsePosition(0)));
return cal;
}
现在我需要将它设置为午夜:
public void setToStartOfDayLocally(GregorianCalendar cal){
????
}
我不太确定我在日历上需要做什么。不幸的是,我们不会在不久的将来搬到JODA的日期。
感谢您,
p>
Andez
我认为此代码可能会解决您的问题。我使用它从当地时区的时间转换到另一个。
public static Date convertLocalDateToDateTimezone(Date localDate,String timezone){
TimeZone localTimeZone = TimeZone.getDefault();
TimeZone timezone = TimeZone.getTimeZone(timezone);
long gmtMillis = localDate.getTime();
long result = gmtMillis + timezone.getOffset(gmtMillis) - localTimeZone.getOffset(gmtMillis);
return new Date(results);
}
希望这有帮助。
All,
Having a hard day in the office with this one... I am trying to workout the start of a day in Local Time, i.e. Midnight 00:00:00, given any calendar date.
Given the following days, I want to calculate the start of the day at local time midnight:
Time Start Day (Local) Start Day (GMT)
2011-03-27 00:00:00 GMT --> 2011-03-27 00:00:00 GMT --> 2011-03-27 00:00:00 GMT
2011-03-27 01:00:00 GMT --> 2011-03-27 00:00:00 GMT --> 2011-03-27 00:00:00 GMT
2011-03-27 02:00:00 GMT --> 2011-03-27 00:00:00 GMT --> 2011-03-27 00:00:00 GMT
2011-04-01 00:00:00 BST --> 2011-04-01 00:00:00 BST --> 2011-03-31 23:00:00 GMT
2011-10-30 00:00:00 BST --> 2011-10-30 00:00:00 BST --> 2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 BST --> 2011-10-30 00:00:00 BST --> 2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 GMT --> 2011-10-30 00:00:00 BST --> 2011-10-29 23:00:00 GMT
2011-11-01 00:00:00 GMT --> 2011-11-01 00:00:00 GMT --> 2011-11-01 00:00:00 GMT
At present, I am parsing the String Time into a GregorianCalendar using a SimpleDateFormat. This gives me the GMT/UTC time to calculate from.
So I have some code which parses the string into a GregorianCalendar:
public GregorianCalendar getCalendar(String dateTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(sdf.parse(dateTime, new ParsePosition(0)));
return cal;
}
Now I need to set it to midnight locally:
public void setToStartOfDayLocally(GregorianCalendar cal) {
????
}
I am not too sure what I need to do at the minute with the Calendar. Unfortunately we are not moving to JODA date in the near future. I am also not accounting for different time zones with my example.
Any thoughts?
Thanks,
Andez
I think this code might solve your issue. I am using it to convert from time in local timezone to another.
public static Date convertLocalDateToDateTimezone( Date localDate, String timezone ) {
TimeZone localTimeZone = TimeZone.getDefault();
TimeZone timezone = TimeZone.getTimeZone( timezone );
long gmtMillis = localDate.getTime();
long result = gmtMillis + timezone.getOffset( gmtMillis ) - localTimeZone.getOffset( gmtMillis );
return new Date( result );
}
Hope this helps.
这篇关于Java日期(日历)计算给定日期时间的本地时间中一天的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!