java日期加減
『壹』 java如何計算日期的加減
第一種,知道日期,如2019091109144
String str=txnTime;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//格式化輸出日期
Date dt = null;
try {
dt = sdf.parse(str);
} catch (ParseException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.YEAR,-1);//日期減1年
rightNow.add(Calendar.MONTH,3);//日期加3個月
rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
rightNow.add(Calendar.SECOND,60);//日期加60秒天
Date dt1=rightNow.getTime();
String reStr = sdf.format(dt1);
System.out.println(reStr);
第二種,自己獲取時間,格式化輸出計算加減
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//格式化輸出日期
Date now = new Date();
long time = 60*1000;//60秒
Date afterDate = new Date(now .getTime() + time);//60秒後的時間
Date beforeDate = new Date(now .getTime() - time);//60秒前的時間
System.out.println(sdf.format(afterDate ));
『貳』 java如何進行時間加減
1.用java.util.Calender來實現
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期
calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//讓日期加1
System.out.println(calendar.get(Calendar.DATE));//加1之後的日期Top
2.用java.text.SimpleDateFormat和java.util.Date來實現
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("今天的日期:"+df.format(d));
System.out.println("兩天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天後的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
GregorianCalendar gc=new GregorianCalendar();
gc.setTime(new Date);
gc.add(field,value);
value為正則往後,為負則往前
field取1加1年,取2加半年,取3加一季度,取4加一周
取5加一天....
『叄』 java日歷加減
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dateInstance = df.parse("1980-01-01");
Date dateInstance2 = df.parse("1981-01-01");
while (!dateInstance2.equals(dateInstance)) {
System.out.println(df.format(dateInstance));
dateInstance.setDate(dateInstance.getDate() + 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
【你的錯誤】
i * 24 * 60 * 60 * 1000 根據自動類型提升原則,計算結果為一個 int 型。
更正 i * 24 * 60 * 60 * 1000L 即可【只要有一個類型是 long 就 OK 了】
【檢查方法】
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
java.util.Date dateInstance = df.parse("1980-01-01");
for (int i = 0; i < 365; i++) {
System.out.println(df.format(new Date(dateInstance.getTime() + i * 24 * 60 * 60 * 1000)));
System.out.println(dateInstance.getTime());
System.out.println((i * 24 * 60 * 60 * 1000));
System.out.println(dateInstance.getTime() + (i * 24 * 60 * 60 * 1000));
System.out.println();
}
} catch (ParseException e) {
e.printStackTrace();
}
【輸出結果】
... ...
1980-01-25
315504000000
2073600000
317577600000
1979-12-07
315504000000
-2134967296 【問題就出自這一步】
313369032704
... ...
【自己試一下吧】
『肆』 java 獲取本機當前時間並對小時任意加減
代碼如下:
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
publicclassDateTestUtil{
publicstaticvoidmain(String[]args)throwsException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
Stringstr="20110823";
Datedt=sdf.parse(str);
CalendarrightNow=Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.YEAR,-1);//日期減1年
rightNow.add(Calendar.MONTH,3);//日期加3個月
rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
Datedt1=rightNow.getTime();
StringreStr=sdf.format(dt1);
System.out.println(reStr);
}
}
註:塵山豎在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 日期加減
使用calendar類,有add方法,裡面可以增加或者減少日期的,輸出來就可以額。
『柒』 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+"分鍾");
****************************************希望能夠幫助到你!************************************************
如果我的回答對你有幫助,
別忘了點擊我的回答下方【選為滿意答案】按鈕。
謝謝!