用Java做时间的减法
2004-11-05 21:12
656 查看
活用 GregorianCalendar 类的 getTimeInMillis() 方法。
注意,取到的值是从1970年1月1日00:00:00开始算起所经过的微秒数。一秒是一千微秒。
下面是自己写的一个例程及运行结果:
import java.util.GregorianCalendar;
class TestClender {
public static void main (String args[]) {
GregorianCalendar ca = new GregorianCalendar();
System.out.println("ca: " + ca);
System.out.println("ca.time: " + ca.getTimeInMillis());
GregorianCalendar ca0 = new GregorianCalendar(2004-1900,11,31,23,59,59);
GregorianCalendar ca1 = new GregorianCalendar(2005-1900,00,01,00,00,02);
long sa = ca1.getTimeInMillis() - ca0.getTimeInMillis();
System.out.println("ca1-ca0: " + sa);
//2004.11.05 added
String old = "20041231235959";
int old_yyyy = Integer.parseInt(old.substring(0,4));
int old_mm = Integer.parseInt(old.substring(4,6));
int old_dd = Integer.parseInt(old.substring(6,8));
int old_hh = Integer.parseInt(old.substring(8,10));
int old_mi = Integer.parseInt(old.substring(10,12));
int old_ss = Integer.parseInt(old.substring(12,14));
String now = "20050101235959";
int now_yyyy = Integer.parseInt(now.substring(0,4));
int now_mm = Integer.parseInt(now.substring(4,6));
int now_dd = Integer.parseInt(now.substring(6,8));
int now_hh = Integer.parseInt(now.substring(8,10));
int now_mi = Integer.parseInt(now.substring(10,12));
int now_ss = Integer.parseInt(now.substring(12,14));
GregorianCalendar gcOld = new GregorianCalendar(
old_yyyy-1900,old_mm-1,old_dd,old_hh,old_mi,old_ss);
GregorianCalendar gcNow = new GregorianCalendar(
now_yyyy-1900,now_mm-1,now_dd,now_hh,now_mi,now_ss);
sa = gcNow.getTimeInMillis() - gcOld.getTimeInMillis();
System.out.println("now-old: " + sa);
}
}
C:/Ebooks/SCJP/MySCJP>java TestClender
ca: java.util.GregorianCalendar[time=1099655689506,areFieldsSet=true,areAllField
sSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Tokyo",offset=32
400000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWee
k=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=10,WEEK_OF_YEAR=45,WEEK_OF_MO
NTH=1,DAY_OF_MONTH=5,DAY_OF_YEAR=310,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=
1,HOUR=8,HOUR_OF_DAY=20,MINUTE=54,SECOND=49,MILLISECOND=506,ZONE_OFFSET=32400000
,DST_OFFSET=0]
ca.time: 1099655689506
ca1-ca0: 3000
now-old: 86400000
注意,取到的值是从1970年1月1日00:00:00开始算起所经过的微秒数。一秒是一千微秒。
下面是自己写的一个例程及运行结果:
import java.util.GregorianCalendar;
class TestClender {
public static void main (String args[]) {
GregorianCalendar ca = new GregorianCalendar();
System.out.println("ca: " + ca);
System.out.println("ca.time: " + ca.getTimeInMillis());
GregorianCalendar ca0 = new GregorianCalendar(2004-1900,11,31,23,59,59);
GregorianCalendar ca1 = new GregorianCalendar(2005-1900,00,01,00,00,02);
long sa = ca1.getTimeInMillis() - ca0.getTimeInMillis();
System.out.println("ca1-ca0: " + sa);
//2004.11.05 added
String old = "20041231235959";
int old_yyyy = Integer.parseInt(old.substring(0,4));
int old_mm = Integer.parseInt(old.substring(4,6));
int old_dd = Integer.parseInt(old.substring(6,8));
int old_hh = Integer.parseInt(old.substring(8,10));
int old_mi = Integer.parseInt(old.substring(10,12));
int old_ss = Integer.parseInt(old.substring(12,14));
String now = "20050101235959";
int now_yyyy = Integer.parseInt(now.substring(0,4));
int now_mm = Integer.parseInt(now.substring(4,6));
int now_dd = Integer.parseInt(now.substring(6,8));
int now_hh = Integer.parseInt(now.substring(8,10));
int now_mi = Integer.parseInt(now.substring(10,12));
int now_ss = Integer.parseInt(now.substring(12,14));
GregorianCalendar gcOld = new GregorianCalendar(
old_yyyy-1900,old_mm-1,old_dd,old_hh,old_mi,old_ss);
GregorianCalendar gcNow = new GregorianCalendar(
now_yyyy-1900,now_mm-1,now_dd,now_hh,now_mi,now_ss);
sa = gcNow.getTimeInMillis() - gcOld.getTimeInMillis();
System.out.println("now-old: " + sa);
}
}
C:/Ebooks/SCJP/MySCJP>java TestClender
ca: java.util.GregorianCalendar[time=1099655689506,areFieldsSet=true,areAllField
sSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Tokyo",offset=32
400000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWee
k=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=10,WEEK_OF_YEAR=45,WEEK_OF_MO
NTH=1,DAY_OF_MONTH=5,DAY_OF_YEAR=310,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=
1,HOUR=8,HOUR_OF_DAY=20,MINUTE=54,SECOND=49,MILLISECOND=506,ZONE_OFFSET=32400000
,DST_OFFSET=0]
ca.time: 1099655689506
ca1-ca0: 3000
now-old: 86400000
相关文章推荐
- java 显示当前的时间
- 遇到的问题------------时间格式转化时java.text.ParseException: Unparseable date: ""异常
- java获取当前系统时间
- JAVA 时间相关
- java中时间类型的问题
- Java中的时间处理
- Java时间帮助类DateUtil
- JAVA获得当前时间的几种方法
- Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime
- Java读取txt文件,计算2011年9月份的通话时间
- Java Calendar 类的时间操作
- java和数据库之间的日期和时间转换
- java向ORACLE插入日期时间
- Java时间串获取
- Java字符串转换为日期和时间比较大小
- Java本地时间与GMT时间的转换
- Java操作日期和时间的文档集锦
- java.util.Date 与 java.sql.Date互转 及 字符串转换为日期时间格式
- java向MySQL插入当前时间
- 当JAVA WEB服务器端有一个执行时间长的方法,应该如何处理?