Android日期工具类

import android.annotation.SuppressLint;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;


public class DateUtil {


public static String[] weekName = { "周日", "周一", "周二", "周三", "周四", "周五",

"周六" };


public static int getMonthDays(int year, int month) {

if (month > 12) {

month = 1;

year += 1;

} else if (month < 1) {

month = 12;

year -= 1;

}

int[] arr = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int days = 0;


if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

arr[1] = 29; // 闰年2月29天

}


try {

days = arr[month - 1];

} catch (Exception e) {

e.getStackTrace();

}


return days;

}


public static int getYear() {

return Calendar.getInstance().get(Calendar.YEAR);

}


public static int getMonth() {

return Calendar.getInstance().get(Calendar.MONTH) + 1;

}


public static int getCurrentMonthDays() {

return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

}


public static int getWeekDay() {

return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

}


public static int[] getPerviousWeekSunday() {

int[] time = new int[3];

Calendar c = Calendar.getInstance();

c.add(Calendar.DATE, -getWeekDay());

time[0] = c.get(Calendar.YEAR);

time[1] = c.get(Calendar.MONTH)+1;

time[2] = c.get(Calendar.DAY_OF_MONTH);

return time;

}


public static int[] getWeekSunday(int year, int month, int day, int pervious) {

int[] time = new int[3];

Calendar c = Calendar.getInstance();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, month);

c.set(Calendar.DAY_OF_MONTH, day);

c.add(Calendar.DAY_OF_MONTH, pervious);

time[0] = c.get(Calendar.YEAR);

time[1] = c.get(Calendar.MONTH )+1;

time[2] = c.get(Calendar.DAY_OF_MONTH);

return time;


}


public static int getWeekDayFromDate(int year, int month) {

Calendar cal = Calendar.getInstance();

cal.setTime(getDateFromString(year, month));

int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;

if (week_index < 0) {

week_index = 0;

}

return week_index;

}


@SuppressLint("SimpleDateFormat")

public static Date getDateFromString(int year, int month) {

String dateString = year + "-" + (month > 9 ? month : ("0" + month))

+ "-01";

Date date = null;

try {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

date = sdf.parse(dateString);

} catch (ParseException e) {

System.out.println(e.getMessage());

}

return date;

}

}


评论