📄 dateutil.java
字号:
/*
* DateUtil.java -
*
* This file is part of the Jawin Project: http://jawinproject.sourceforge.net/
*
* Please consult the LICENSE file in the project root directory,
* or at the project site before using this software.
*/
/* $Id: DateUtil.java,v 1.3 2004/06/14 19:35:13 arosii_moa Exp $ */
package org.jawin.io;
import java.util.Calendar;
import java.util.Date;
/**
* Marshalling utility methods for handling the platform impedance between
* dates in Win32 and Java.
* <ul>
* <li>Win32: 0 is Dec 30 1899</li>
* <li>Java: 0 is Jan 1 1970</li>
* </ul>
*
* @version $Revision: 1.3 $
* @author Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
*/
public class DateUtil {
/**
* private constructor to avoid instantiation, as this class only contains
* static methods.
*/
private DateUtil() {
// never called
}
private static final int MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24;
//Dec 30 1899 is 0 in Win32 date
//Jan 1 1970 is 0 in Java date
//difference between the two is 70 years + 2 days
//leap years are 1904, 1908, etc. = 17 leaps
//(365 * 70) + 17 + 2
private static final int SUN_MINUS_MICROSOFT = 25569;
/**
* @return the java milliseconds since 1970 from a win32 date.
*/
public static long getJavaDate(double date) {
double javaNormalizedDate = date - SUN_MINUS_MICROSOFT;
long millis = Math.round(86400000L * javaNormalizedDate);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(millis));
return millis - cal.get(Calendar.ZONE_OFFSET) - cal.get(Calendar.DST_OFFSET);
}
/**
* @return the win32 date from a java.lang.Date.
*/
public static double getWin32Date(java.util.Date dt) {
return getWin32Date(dt.getTime());
}
/**
* @return the win32 date from a java milliseconds since 1970.
*/
public static double getWin32Date(long dt) {
Date date = new Date(dt);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
long millis = date.getTime()
+ cal.get(Calendar.ZONE_OFFSET)
+ cal.get(Calendar.DST_OFFSET);
return (double) millis / MILLISECONDS_IN_DAY + SUN_MINUS_MICROSOFT;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -