当前位置: 技术问答>java相关
在线等待,马上给分!java中如何获取系统时间?
来源: 互联网 发布时间:2015-06-09
本文导语: java中如何获取系统时间? | 这是一个计算时间差的程序,改动以下就可以了!!! 好运~~~~ package ppmsys.pre.client; import java.util.*; import java.text.*; /** SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd")...
java中如何获取系统时间?
|
这是一个计算时间差的程序,改动以下就可以了!!! 好运~~~~
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
}
}
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
}
}
|
给你一个现成的类
package com.hoten.util;
import java.util.*;
public class getDate {
public getDate() {
}
public static String get1yyyymmddhhmmssDate(){
Calendar date = java.util.Calendar.getInstance();
String reStr = String.valueOf(date.get(Calendar.YEAR)) + addZero(date.get(Calendar.MONTH) + 1) +
addZero(date.get(Calendar.DATE)) + addZero(date.get(Calendar.HOUR)) +
addZero(date.get(Calendar.MINUTE)) + addZero(date.get(Calendar.SECOND));
return reStr;
}
public static String get14Date(){
Calendar date = java.util.Calendar.getInstance();
int intMSecond = date.get(Calendar.MILLISECOND);
String strMSecond;
if (intMSecond
package com.hoten.util;
import java.util.*;
public class getDate {
public getDate() {
}
public static String get1yyyymmddhhmmssDate(){
Calendar date = java.util.Calendar.getInstance();
String reStr = String.valueOf(date.get(Calendar.YEAR)) + addZero(date.get(Calendar.MONTH) + 1) +
addZero(date.get(Calendar.DATE)) + addZero(date.get(Calendar.HOUR)) +
addZero(date.get(Calendar.MINUTE)) + addZero(date.get(Calendar.SECOND));
return reStr;
}
public static String get14Date(){
Calendar date = java.util.Calendar.getInstance();
int intMSecond = date.get(Calendar.MILLISECOND);
String strMSecond;
if (intMSecond