import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
//日期相减算出秒的算法
Date date1 = new SimpleDateFormat("yyyy-mm-dd").parse("2005-06-08");
Date date2 = new SimpleDateFormat("yyyy-mm-dd").parse("2006-06-12");
long l = date1.getTime()-date2.getTime()>0 ? date1.getTime()-date2.getTime():
date2.getTime()-date1.getTime();
//System.out.println(l/1000+"秒");
//日期相减得到相差的日期
long day = (date1.getTime()-date2.getTime())/(24*60*60*1000)>0 ? (date1.getTime()-date2.getTime())/(24*60*60*1000):
(date2.getTime()-date1.getTime())/(24*60*60*1000);
long day1=date1.getTime();
Date sb = new Date();
sb.setTime(day1);
System.out.println(Ds(30));
// System.out.println("相差的日期: " +day);
}
public static String Ds(int days){
SimpleDateFormat form=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_YEAR);
calendar.set(Calendar.DAY_OF_YEAR, day - days);
Date cc = calendar.getTime();
return form.format(cc);
}
}
public class aaa {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
java.util.Date date = new java.util.Date();
java.text.Format fmtDate = new java.text.SimpleDateFormat("yyyy-MM");
String START = fmtDate.format(date)+"-01";
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
gc.setTime(df.parse(START));
gc.add(2,-1);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DAY_OF_MONTH));
String END = df.format(gc.getTime());
System.out.println("时间:"+START+"~"+END);
}
}
package com;
public class aaa {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
java.util.Date date = new java.util.Date();
java.text.Format fmtDate = new java.text.SimpleDateFormat("yyyy-MM");
String START = fmtDate.format(date)+"-01";
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
gc.setTime(df.parse(START));
gc.add(2,-1);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DATE));
String END = df.format(gc.getTime());
gc.setTime(df.parse(START));
gc.add(5,-1);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DATE));
START = df.format(gc.getTime());
System.out.println("时间:"+END+"~"+START);
}
}
package com;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class aaa {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
/*java.util.Date date = new java.util.Date();
java.text.Format fmtDate = new java.text.SimpleDateFormat("yyyy-MM");
String START = fmtDate.format(date)+"-01";
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
gc.setTime(df.parse(START));
gc.add(2,-1);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DATE));
String END = df.format(gc.getTime());
gc.setTime(df.parse(START));
gc.add(5,-1);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DATE));
START = df.format(gc.getTime());
System.out.println("时间:"+END+"~"+START);*/
java.util.Date date = new java.util.Date();
java.text.Format fmtDate = new java.text.SimpleDateFormat("yyyy");
String START = fmtDate.format(date)+"-01"+"-01";
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
gc.setTime(df.parse(START));
gc.add(1,-1);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DATE));
String END = df.format(gc.getTime());
System.out.println("时间:"+END+"~"+START);
}
}