当前位置: 技术问答>想要一份Jbuilder7注册文件key.txt,一百分,多谢
iis7站长之家
我想要两个时间的差值
来源: 互联网 发布时间:2015-06-11
本文导语: 我用的是Calendar类,若觉得我用的类不好,请告示我更好的,我会虚心接受。 请告示我如何求:(包括具体什么类,最好有代码。) 1.日期之间差值 2.时分秒的差值 谢谢! | import java.util.*; 取值你的JDK...
我用的是Calendar类,若觉得我用的类不好,请告示我更好的,我会虚心接受。
请告示我如何求:(包括具体什么类,最好有代码。)
1.日期之间差值
2.时分秒的差值
谢谢!
请告示我如何求:(包括具体什么类,最好有代码。)
1.日期之间差值
2.时分秒的差值
谢谢!
|
import java.util.*;
取值你的JDK版本是多少,在JDK1.4下完全没问题
取值你的JDK版本是多少,在JDK1.4下完全没问题
|
package ppmsys.pre.client;
import java.util.*;
import java.text.*;
/**
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str="20011230";
Date dt=sdf.parse(str,new ParsePosition(0));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE,2);//你要加减的日期
Date dt1=rightNow.getTime();
String reStr=sdf.format(dt1,"",new FieldPosition(0));
System.out.println(reStr);
*
*/
public class ThreeDaysAgo
{
public static void main(String args[])
{
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime ();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println ("Today: "+df.format (date));
calendar.set (Calendar.DATE,
calendar.get (Calendar.DATE)-3);
date = calendar.getTime ();
System.out.println ("Three days ago: "+df.format (date));
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// if no ids were returned, something is wrong. get out.
if (ids.length == 0)
System.exit(0);
// begin output
System.out.println("Current Time");
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar1 = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar1.setTime(trialTime);
// print out a bunch of interesting things
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar1.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar1.set(Calendar.HOUR, 3);
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
}
}
记得给我分哦 :)
import java.util.*;
import java.text.*;
/**
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str="20011230";
Date dt=sdf.parse(str,new ParsePosition(0));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE,2);//你要加减的日期
Date dt1=rightNow.getTime();
String reStr=sdf.format(dt1,"",new FieldPosition(0));
System.out.println(reStr);
*
*/
public class ThreeDaysAgo
{
public static void main(String args[])
{
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime ();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println ("Today: "+df.format (date));
calendar.set (Calendar.DATE,
calendar.get (Calendar.DATE)-3);
date = calendar.getTime ();
System.out.println ("Three days ago: "+df.format (date));
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// if no ids were returned, something is wrong. get out.
if (ids.length == 0)
System.exit(0);
// begin output
System.out.println("Current Time");
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar1 = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar1.setTime(trialTime);
// print out a bunch of interesting things
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar1.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar1.set(Calendar.HOUR, 3);
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
}
}
记得给我分哦 :)
|
对不起,我没有做过这样的内容,我现在主要是用javax.swing.*,做程序,惭愧!!