package com.hefeng.test;
import Java.text.DateFormat;
import Java.text.ParsePosition;
import Java.text.SimpleDateFormat;
import Java.util.Calendar;
import Java.util.Date;
import Java.util.GregorianCalendar;
public class TimeTest {
//用來全局控制上一周,本周,下一周的周數變化
private int weeks = 0;
private int MaxDate;//一月最大天數
private int MaxYear;//一年最大天數
public static void main(String[] args) {
TimeTest tt = new TimeTest();
System.out.println("獲取當天日期:"+tt.getNowTime("yyyy-MM-dd"));
System.out.println("獲取本周一日期:"+tt.getMondayOFWeek());
System.out.println("獲取本周日的日期~:"+tt.getCurrentWeekday());
System.out.println("獲取上周一日期:"+tt.getPreviousWeekday());
System.out.println("獲取上周日日期:"+tt.getPreviousWeekSunday());
System.out.println("獲取下周一日期:"+tt.getNextMonday());
System.out.println("獲取下周日日期:"+tt.getNextSunday());
System.out.println("獲得相應周的周六的日期:"+tt.getNowTime("yyyy-MM-dd"));
System.out.println("獲取本月第一天日期:"+tt.getFirstDayOfMonth());
System.out.println("獲取本月最後一天日期:"+tt.getDefaultDay());
System.out.println("獲取上月第一天日期:"+tt.getPreviousMonthFirst());
System.out.println("獲取上月最後一天的日期:"+tt.getPreviousMonthEnd());
System.out.println("獲取下月第一天日期:"+tt.getNextMonthFirst());
System.out.println("獲取下月最後一天日期:"+tt.getNextMonthEnd());
System.out.println("獲取本年的第一天日期:"+tt.getCurrentYearFirst());
System.out.println("獲取本年最後一天日期:"+tt.getCurrentYearEnd());
System.out.println("獲取去年的第一天日期:"+tt.getPreviousYearFirst());
System.out.println("獲取去年的最後一天日期:"+tt.getPreviousYearEnd());
System.out.println("獲取明年第一天日期:"+tt.getNextYearFirst());
System.out.println("獲取明年最後一天日期:"+tt.getNextYearEnd());
System.out.println("獲取本季度第一天到最後一天:"+tt.getThisSeasonTime(11));
System.out.println("獲取兩個日期之間間隔天數2008-12-1~2008-9.29:"+TimeTest.getTwoDay("2008-12-1","2008-9-29"));
}
public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
Java.util.Date date = myFormatter.parse(sj1);
Java.util.Date mydate = myFormatter.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}
public static String getWeek(String sdate) {
// 再轉換為時間
Date date = TimeTest.strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
// int hour=c.get(Calendar.DAY_OF_WEEK);
// hour中存的就是星期幾了,其范圍 1~7
// 1=星期日 7=星期六,其他類推
return new SimpleDateFormat("EEEE").format(c.getTime());
}
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static long getDays(String date1, String date2) {
if (date1 == null || date1.equals(""))
return 0;
if (date2 == null || date2.equals(""))
return 0;
// 轉換為標准時間
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
Java.util.Date date = null;
Java.util.Date mydate = null;
try {
date = myFormatter.parse(date1);
mydate = myFormatter.parse(date2);
} catch (Exception e) {
}
long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
// 計算當月最後一天,返回字符串
public String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//設為當前月的1號
lastDate.add(Calendar.MONTH,1);//加一個月,變為下月的1號
lastDate.add(Calendar.DATE,-1);//減去一天,變為當月最後一天
str=sdf.format(lastDate.getTime());
return str;
}
// 上月第一天
public String getPreviousMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//設為當前月的1號
lastDate.add(Calendar.MONTH,-1);//減一個月,變為下月的1號
//lastDate.add(Calendar.DATE,-1);//減去一天,變為當月最後一天
str=sdf.format(lastDate.getTime());
return str;
}
//獲取當月第一天
public String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE,1);//設為當前月的1號
str=sdf.format(lastDate.getTime());
return str;
}