Android Calendar的学习与运用
[java]
mport java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Android Calendar的运用
* @author Administrator
*
*/
public class CalendarUtil {
private int weeks = 0;// 用来全局控制 上一周,本周,下一周的周数变化
private int MaxDate; // 一月最大天数
private int MaxYear; // 一年最大天数
public static void main(String[] args) {
CalendarUtil tt = new CalendarUtil();
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.getThisSeasonFirstTime(11));
System.out.println("获取本季度最后一天:" + tt.getThisSeasonFinallyTime(11));
System.out.println("获取两个日期之间间隔天数2008-12-1~2008-9.29:"
+ CalendarUtil.getTwoDay("2008-12-1", "2008-9-29"));
System.out.println("获取当前月的第几周:" + tt.getWeekOfMonth());
System.out.println("获取当前年份:" + tt.getYear());
System.out.println("获取当前月份:" + tt.getMonth());
System.out.println("获取今天在本年的第几天:" + tt.getDayOfYear());
System.out.println("获得今天在本月的第几天(获得当前日):" + tt.getDayOfMonth());
System.out.println("获得今天在本周的第几天:" + tt.getDayOfWeek());
System.out.println("获得半年后的日期:"
+ tt.convertDateToString(tt.getTimeYearNext()));
}
public static int getYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
public static int getMonth() {
return Calendar.getInstance().get(Calendar.MONTH) + 1;
}
public static int getDayOfYear() {
return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
}
public static int getDayOfMonth() {
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
public static int getDayOfWeek() {
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
}
public static int getWeekOfMonth() {
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH);
}
public static Date getTimeYearNext() {
Calendar.getInstance().add(Calendar.DAY_OF_YEAR, 183);
return Calendar.getInstance().getTime();
}
public static String convertDateToString(Date dateTime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(dateTime);
}
public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
&
补充:移动开发 , Android ,