1.日期 = 上週 下週
2.日期 = 前一天 後一天
3.日期 = 相差天數
-------------------------上週 下週------------------------------------------
package com;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test01 {
public static void main(String args[]){
int i=0;
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
int n = i;
String monday;
cal.add(Calendar.DATE, n*7);
monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
int n1 = i-1;
String monday1;
cal1.add(Calendar.DATE, n1*7);
monday1 = new SimpleDateFormat("yyyy-MM-dd").format(cal1.getTime());
System.out.println(monday1+" : "+monday);
}
}
-------------------------前一天 後一天-----------------------------------
package com;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test02 {
public static void main(String[] args) throws ParseException {
String Date_Text ="2013-03-31";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(Date_Text));
cal.add(Calendar.DATE, -1);//減一天
//cal.add(Calendar.MONTH, n);//增加一个月
System.out.println(" 增加一天 : "+ sdf.format(cal.getTime()));
}
}
--------------------------相差天數-----------------------------------
package com;
import java.util.Date;
public class Test03 {
public static void main(String[] args) {
String s1 = "2013-04-01";
String s2 = "2013-03-27";
String t1 = s1.replace('-','/');
String t2 = s2.replace('-','/');
try{
Date dt1= new Date(t1);
Date dt2= new Date(t2);
System.out.println("dt1="+dt1.getTime());
System.out.println("dt2="+dt2.getTime());
long l = dt1.getTime() - dt2.getTime();
System.out.println(l);
System.out.println("相差天數 "+l/60/60/1000/24);
}catch(Exception e){
System.out.println("exception"+e.toString());
}
}
}
沒有留言:
張貼留言