关于java代码日期减1的信息
Java Date类型 减一个月怎么做
String startDateStr = "";
创新互联-专业网站定制、快速模板网站建设、高性价比白云鄂网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式白云鄂网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖白云鄂地区。费用合理售后完善,十载实体公司更值得信赖。
String endDateStr = "";
TimeZone tzES2 = TimeZone.getTimeZone("GMT+8");
Calendar calES2 = Calendar.getInstance(tzES2);
Calendar ca = Calendar.getInstance();
Date now = ca.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat.setCalendar(calES2);
ca.set(Calendar.DATE, ca.getActualMinimum(Calendar.DAY_OF_MONTH));//设置当前日期为当前月的第一天
now = ca.getTime();
startDateStr = dateFormat.format(now);
ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DAY_OF_MONTH));//设置当前日期为当前月的最后一天
now = ca.getTime();
endDateStr = dateFormat.format(now);
System.out.println(startDateStr);
System.out.println(endDateStr);
ca.add(Calendar.MONTH, -1);//设置日期为当前日期加上-1个月(即减去1个月)的日期。
后面的操作同上!
java的前台传来一个Date日期的值,我想做的是:让这个日期减一天,比如页面上传来的是2013-1-1,那么减一天为
最简单的做法是:
// 假如这是前台传来的Date时间
Date dt;
// 1天的毫秒数
long oneDayTime = 1000*3600*24;
// 这个now就是减1天的时间了
Date nowTime = new Date(dt.getTime() - oneDayTime);
原理就是用Date的毫秒数做运算,最后再将运算出来的毫秒数再转换成时间。
用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时间加减
附上代码:
Date date = new Date("2014/1/10 18:20");
Date date2 = new Date("2014/1/11 3:5");
long temp = date2.getTime() - date.getTime(); //相差毫秒数
long hours = temp / 1000 / 3600; //相差小时数
long temp2 = temp % (1000 * 3600);
long mins = temp2 / 1000 / 60; //相差分钟数
System.out.println("date2 与 date 相差" + hours + "小时"+ mins + "分钟");
****************************************希望能够帮助到你!************************************************
如果我的回答对你有帮助,
别忘了点击我的回答下方【选为满意答案】按钮。
谢谢!
分享题目:关于java代码日期减1的信息
网站地址:http://azwzsj.com/article/hcpepe.html