from http://javaalmanac.com/egs/java.util/CompDates.htmlCalendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);
// Determine which is earlier
boolean b = xmas.after(newyears); // false
b = xmas.before(newyears); // true
// Get difference in milliseconds
long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();
// Get difference in seconds
long diffSecs = diffMillis/(1000); // 604800
// Get difference in minutes
long diffMins = diffMillis/(60*1000); // 10080
// Get difference in hours
long diffHours = diffMillis/(60*60*1000); // 168
// Get difference in days
long diffDays = diffMillis/(24*60*60*1000); // 7
Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);
// Determine which is earlier
boolean b = xmas.after(newyears); // false
b = xmas.before(newyears); // true
// Get difference in milliseconds
long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();
// Get difference in seconds
long diffSecs = diffMillis/(1000); // 604800
// Get difference in minutes
long diffMins = diffMillis/(60*1000); // 10080
// Get difference in hours
long diffHours = diffMillis/(60*60*1000); // 168
// Get difference in days
long diffDays = diffMillis/(24*60*60*1000); // 7
解决方案 »
- 刚学习JAVA的String遇到一点小问题。求解释
- CipherInputStream转换成FileInputStream
- 如何给swing表格某一格设置背景
- 怎么记录当前系统时间
- Vector里面加入很多值
- 高分,请问各位大侠,哪位有李兴华老师上课的J2SE的上课笔记?
- 请教java正则转义字符的问题.
- 有没有什么方法能得到这个CELL的类型的
- 如何调用内部类的方法啊,在线等.
- 各位大哥,请教关于system.out的问题
- 为什么我用AppletViewer BestWish.htm可以看到效果,可是在IE里却总是class not found,如何解决???急!!!
- 请问webloigc 的API的帮助文档,在什么位置我,没找到谢谢了。
因为getTimeInMillis()这个函数是保护类,编译没通过。请大家帮我想想办法!
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
}
}
记得给我分哦 :)
class TestTime
{
public static void main (String[] args)
{ Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);
// Determine which is earlier
boolean b = xmas.after(newyears); // false
b = xmas.before(newyears); // true
// Get difference in milliseconds
long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();
// Get difference in seconds
long diffSecs = diffMillis/(1000); // 604800
// Get difference in minutes
long diffMins = diffMillis/(60*1000); // 10080
// Get difference in hours
long diffHours = diffMillis/(60*60*1000); // 168
// Get difference in days
long diffDays = diffMillis/(24*60*60*1000); // 7 }
}