Java??λ???????
???????????? ???????[ 2014/4/18 10:54:41 ] ????????Java ??
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("??????o?????:"+tt.getDefaultDay());
System.out.println("??????μ?????:"+tt.getPreviousMonthFirst());
System.out.println("??????o??????:"+tt.getPreviousMonthEnd());
System.out.println("??????μ?????:"+tt.getNextMonthFirst());
System.out.println("??????o?????:"+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;
}
// ?????o??????????
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);//???????????o?
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);//???????????o?
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;
}
// ?????????????????
public String getCurrentWeekday() {
weeks = 0;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE?? mondayPlus+6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11