当前位置:首页 » 编程语言 » java日期加减

java日期加减

发布时间: 2024-11-04 15:50:02

‘壹’ 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+"分钟");



****************************************希望能够帮助到你!************************************************
如果我的回答对你有帮助,
别忘了点击我的回答下方【选为满意答案】按钮。
谢谢!

热点内容
eclipsejar源码乱码 发布:2024-11-23 10:01:33 浏览:144
oracle导入数据库数据 发布:2024-11-23 09:57:09 浏览:795
高访问网址 发布:2024-11-23 09:53:02 浏览:519
android内置apk 发布:2024-11-23 09:46:18 浏览:319
邮箱服务器搭建windows 发布:2024-11-23 09:44:46 浏览:559
安卓如何强制关闭 发布:2024-11-23 09:43:05 浏览:447
linux怎么安装samba服务器 发布:2024-11-23 09:42:26 浏览:427
php开源建站 发布:2024-11-23 09:33:57 浏览:44
医工院数据库 发布:2024-11-23 09:29:26 浏览:360
金税盘安全接入服务器地址 发布:2024-11-23 09:18:00 浏览:24