1.用java.util.Calender来实现
在成都网站设计、做网站中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联公司专业成都网站建设10多年了,客户满意度97.8%,欢迎成都创新互联客户联系。
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中对日期的加减操作
*gc.add(1,-1)表示年份减一.
*gc.add(2,-1)表示月份减一.
*gc.add(3.-1)表示周减一.
*gc.add(5,-1)表示天减一.
*以此类推应该可以精确的毫秒吧.没有再试.大家可以试试.
*GregorianCalendar类的add(int field,int amount)方法表示年月日加减.
*field参数表示年,月.日等.
*amount参数表示要加减的数量.
第一种,知道日期,如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 ));
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烫伤中心提供。稍微再修改下就可以
代码如下:
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是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
主要组成
Java由四方面组成:
●Java编程语言,即语法。
●Java文件格式,即各种文件夹、文件的后缀。
●Java虚拟机(JVM),即处理*.class文件的解释器。
●Java应用程序接口(JavaAPI)。
体系
Java分为三个体系,分别为JavaSE(J2SE,Java2PlatformStandardEdition,标准版),
JavaEE(J2EE,Java2Platform,EnterpriseEdition,企业版),
JavaME(J2ME,Java2PlatformMicroEdition,微型版)。