日期加減java
㈠ java時間加減
Datedate=newDate("2014/1/1018:20");
Datedate2=newDate("2014/1/113:5");
longtemp=date2.getTime()-date.getTime();//相差毫秒數
longhours=temp/1000/3600;//相差小時數
longtemp2=temp%(1000*3600);
longmins=temp2/1000/60;//相差分鍾數
System.out.println("date2與date相差"+hours+"小時"+mins+"分鍾");
****************************************希望能夠幫助到你!************************************************
如果我的回答對你有幫助,
別忘了點擊我的回答下方【選為滿意答案】按鈕。
謝謝!
㈡ java中時間的加減法怎麼做
最簡單的方法應該是,換成毫秒,直接加減之後再換回24進制時
㈢ java里日期如何相加減如何計算兩個時間的差值
date1.getTime()-date2.getTime()
㈣ java 中 日期如何相加減
這個東西很簡單。
現在是2004-03-26 13:31:40
過去是:2004-01-02 11:30:24
要獲得兩個日期差,差的形式為:XX天XX小時XX分XX秒
方法一:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date d1 = df.parse("2004-03-26 13:31:40");
Date d2 = df.parse("2004-01-02 11:30:24");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
}
catch (Exception e)
{
}
方法二:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = df.parse("2004-03-26 13:31:40");
java.util.Date date=df.parse("2004-01-02 11:30:24");
long l=now.getTime()-date.getTime();
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
System.out.println(""+day+"天"+hour+"小時"+min+"分"+s+"秒");
方法三:
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date begin=dfs.parse("2004-01-02 11:30:24");
java.util.Date end = dfs.parse("2004-03-26 13:31:40");
long between=(end.getTime()-begin.getTime())/1000;//除以1000是為了轉換成秒
long day1=between/(24*3600);
long hour1=between%(24*3600)/3600;
long minute1=between%3600/60;
long second1=between%60/60;
System.out.println(""+day1+"天"+hour1+"小時"+minute1+"分"+second1+"秒");
㈤ 如何實現java 日期加減
使用calendar類,有add方法,裡面可以增加或者減少日期的,輸出來就可以額。
㈥ 用java實現日期類的加減
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
public class CalendarExample {
private static void CalendarTimemethod() {
Date date = Calendar.getInstance().getTime();
System.out.println("Current date and time is: " + date);
System.out.println();
}
private static void SimpleDateFormatmethod() {
Calendar date = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current date and time in simple date format: "
+ dateformatter.format(date.getTime()));
System.out.println();
}
private static void Adddates() {
System.out.println("Performing operations on calendar dates.");
// Get today's date
Calendar date = Calendar.getInstance();
Calendar cldr;
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, -(365 * 2));
System.out.println("Before two years it was: "
+ dateformatter.format(cldr.getTime()));
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, +5);
System.out.println("After five years it will be: "
+ dateformatter.format(cldr.getTime()));
System.out.println();
}
private static void DateDifference() {
System.out.println("Difference between two dates");
Date startDate1 = new GregorianCalendar(2005, 02,
25, 14, 00).getTime();
Date endDate1 = new Date();
;
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println(" Difference between " + endDate1);
System.out.println(" and " + startDate1 + " is " + (diff /
(1000L * 60L * 60L * 24L)) + " days.");
System.out.println();
}
private static void Getcalendermethods() {
System.out.println("Various get methods of the calendar class:");
Calendar calender = Calendar.getInstance();
System.out.println("Year : "
+ calender.get(Calendar.YEAR));
System.out.println("Month : "
+ calender.get(Calendar.MONTH));
System.out.println("Day of Month : "
+ calender.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of Week : "
+ calender.get(Calendar.DAY_OF_WEEK));
System.out.println("Day of Year : "
+ calender.get(Calendar.DAY_OF_YEAR));
System.out.println("Week of Year : "
+ calender.get(Calendar.WEEK_OF_YEAR));
System.out.println("Week of Month : "
+ calender.get(Calendar.WEEK_OF_MONTH));
System.out.println("Day of the Week in Month : "
+ calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : " + calender.get(Calendar.HOUR));
System.out.println("AM PM : " + calender.get(Calendar.AM_PM));
System.out.println("Hour of the Day : "
+ calender.get(Calendar.HOUR_OF_DAY));
System.out.println("Minute : " + calender.get(Calendar.MINUTE));
System.out.println("Second : " + calender.get(Calendar.SECOND));
System.out.println();
}
public static void main(String[] args) {
System.out.println();
CalendarTimemethod();
SimpleDateFormatmethod();
Adddates();
DateDifference();
Getcalendermethods();
}
}
以上答案有林氏120燙傷中心提供。稍微再修改下就可以
㈦ java 兩個日期相減的怎麼做
對已日期相減,最高效的做法就是將二者都轉換成毫秒,相減之後再根據你的需求進行單位轉換,比如你想顯示相差的秒數,就除以1000,以此類推,翠花,上代碼:
/*隨便選兩個時間*/
Stringd1="2015-04-17";
Stringd2="2015-06-17";
/*先轉成毫秒並求差*/
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
longm=sdf.parse(d2).getTime()-sdf.parse(d1).getTime();
/*根據你的需求進行單位轉換*/
System.out.println("相差毫秒數:"+m);
System.out.println("相差天數:"+(m/(1000*60*60*24)));
要注意的地方:
時間格式可能有很多種,比如20150611或者2015年6月11日等等。所以你需要以對應的方式來初始化SimpleDateFormat對象。
例如20150611,就要寫成:
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
SimpleDateFormat類是非線程安全的,所以在高並發下需要加同步鎖,否則會出現靈異事件。
㈧ java中兩日期用什麼運算符相加減
code:
java.util.Calendar c = Calendar.getInstance();
c.setTime(new java.util.Date());
System.out.println(c.getTime());
// + 1年,1月,1天,
c.add(Calendar.YEAR, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DAY_OF_YEAR, 1);
// - 1時,10分,10秒
c.roll(Calendar.HOUR, 1);
c.roll(Calendar.MINUTE, 10);
c.roll(Calendar.SECOND, 10);
System.out.println(c.getTime());
output:
Wed Aug 24 10:55:37 CST 2011
Tue Sep 25 11:05:47 CST 2012
㈨ java 日期加減
你得到一個時間對象,Data date = new Date();用date對象有date.什麼方法去了獲得當前時間月份,記得不是很清楚了,你試一下就知道了,然後用個String對象去保存嗎,操作那個String對象總知道嗎,然後再給date賦值,還有一種是使用SimpleDataFormat對象轉換時間,自己去試一試
㈩ java 中 日期怎麼相加減
JAVA Calendar類提供了2個方法:add() 和 roll()。
Calendar cal = Calendar.getInstance();
add(f, delta) 將 delta 添加到 f 欄位中。這等同於調用 set(f, get(f) + delta),
roll(f, delta) 將 delta 添加到 f 欄位中,但不更改更大的欄位。這等同於調用 add(f, delta)
如果要計算2個日期之間的差距,可以取得自1970 年 1 月 1 日的 00:00:00.000各自的毫秒數字,然後相減
long millionSecondsForDate1 = date1.getTime();
long millionSecondsForDate2 = date2.getTime();
long julianSeconds = millionSecondsForDate1 - millionSecondsForDate2 ;
然後你可以轉化為你想要得時間,注意毫秒和秒之間進位是1000
1秒 = 1000毫秒