读书人

日期操作种

发布时间: 2012-11-01 11:11:32 作者: rapoo

日期操作类

package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

?

public class DateUtil {

?public static String STARTDATE = "startDate";
?public static String ENDDATE = "endDate";
?public static String WEEKNUM = "weekNum";

?

?public static String getStrOfDate(Date date) {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
??return sdf.format(date);
?}

?

?public static String getStrOfMonth(Date date) {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
??return sdf.format(date);
?}

?

?public static String getStrOfYear(Date date) {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
??return sdf.format(date);
?}

?

?// 取某一天的一个月后的日期
?public static String getNextMonthDay(String str) throws Exception {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(sdf.parse(str));
??gc.add(Calendar.MONTH, +1);
??return getStrOfDate(gc.getTime());
?}

?

?// 取某个月的上个月
?public static String getPreMonth(String str) throws Exception {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(sdf.parse(str));
??gc.add(Calendar.MONTH, -1);
??return getStrOfDate(gc.getTime());
?}

?

?// 取某个月的下个月
?public static String getNextMonth(String str) throws Exception {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(sdf.parse(str));
??gc.add(Calendar.MONTH, +1);
??return getStrOfDate(gc.getTime());
?}

?

?// 取某一天的一周前的日期
?public static String getPreWeekDay(String str) throws Exception {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(sdf.parse(str));
??gc.add(Calendar.DATE, -6);
??return getStrOfDate(gc.getTime());
?}

?

?// 取某一天的一周前的日期
?public static String getPWeekDay(String str) throws Exception {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(sdf.parse(str));
??gc.add(Calendar.DATE, -6);
??return getStrOfDate(gc.getTime());
?}

?

?public static String getCurrentStrDate() {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
??Date date = new Date(System.currentTimeMillis());
??return sdf.format(date);
?}

?

?public static String getCurrentStrMonth() {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
??Date date = new Date(System.currentTimeMillis());
??return sdf.format(date);
?}

?

?// 取当年
?public static String getCurrentStrYear() {
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
??Date date = new Date(System.currentTimeMillis());
??return sdf.format(date);
?}

?

?// 取上一年
?public static String getPreYear() {
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(new Date(System.currentTimeMillis()));
??gc.add(Calendar.YEAR, -1);
??return getStrOfYear(gc.getTime());
?}

?

?// 取上个月
?public static String getPreMonth() {
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(new Date(System.currentTimeMillis()));
??gc.add(Calendar.MONTH, -1);
??return getStrOfMonth(gc.getTime());
?}

?

?// 取一个月前的日期
?public static String getPreMonthDay() {
??GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
??gc.setTime(new Date(System.currentTimeMillis()));
??gc.add(Calendar.MONTH, -1);
??gc.add(Calendar.DATE, 1);
??return getStrOfDate(gc.getTime());
?}

?

?// 取前一天
?public static String getStrOfPreDate() {
??GregorianCalendar gc = new GregorianCalendar();
??gc.add(GregorianCalendar.DATE, -1);
??return getStrOfDate(gc.getTime());
?}

?

?// 取当月第一天
?public static String getStrOfFirstDate() {
??GregorianCalendar gc = new GregorianCalendar();
??gc.add(GregorianCalendar.DATE,
????-gc.get(GregorianCalendar.DAY_OF_MONTH) + 1);
??return getStrOfDate(gc.getTime());
?}

?

?// 取当月最后一天
?public static String getStrOfLastDate() {
??GregorianCalendar gc = new GregorianCalendar();
??gc.add(GregorianCalendar.MONTH, 1);
??gc.add(GregorianCalendar.DATE, -gc.get(GregorianCalendar.DAY_OF_MONTH));
??return getStrOfDate(gc.getTime());
?}

?

?/**
? * 获取日期所在周的第一天及最后一天,并求出其为一年中的第几周
? *
? * @param date
? * @return
? */
?public static Map<String, String> getDaysOfWeek(String date) {
??String startDate = "";
??String endDate = "";
??String weekNum = "";
??Calendar cal = Calendar.getInstance();
??Map<String, String> dates = new HashMap<String, String>();

??// 获取日期格式化对象
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

??// 将字符串解析为Calendar
??try {
???cal.setTime(sdf.parse(date));
??} catch (Exception e) {
???e.printStackTrace();
??}

??// 获取当前日期为一周当中第几天(MONDAY=2,TUESDAY=3,WEDNESDAY=4,THURSDAY=5,FRIDAY=6,SATURDAY=7,SUNDAY=1)
??Integer day = cal.get(Calendar.DAY_OF_WEEK);

??// 获取当前规则下,一周的第一天,将周六做为一周的开始
??if (day == 7) {
???startDate = sdf.format(cal.getTime());
??} else {
???cal.add(Calendar.DATE, -day);
???startDate = sdf.format(cal.getTime());
??}

??// 获取当前规则下,一周的最后一天
??cal.add(Calendar.DATE, 6);
??endDate = sdf.format(cal.getTime());

??// 将周六做为一周的开始
??cal.setFirstDayOfWeek(Calendar.SATURDAY);
??// 获取当前日期是一年中的第几周
??weekNum = Integer.toString(cal.get(Calendar.WEEK_OF_YEAR));

??// 将获取的数据存入Map中
??dates.put(STARTDATE, startDate);
??dates.put(ENDDATE, endDate);
??dates.put(WEEKNUM, weekNum);

??return dates;
?}

?

?/**
? * 获取日期所在周的第一天及最后一天,并求出其为一年中的第几周
? *
? * @param date
? * @return
? */
?public static Map<String, String> getDaysOfWeek(Date date) {
??String startDate = "";
??String endDate = "";
??String weekNum = "";
??Calendar cal = Calendar.getInstance();
??cal.setTime(date);

??Map<String, String> dates = new HashMap<String, String>();

??// 获取日期格式化对象
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

??// 将周六做为一周的开始(MONDAY=2,TUESDAY=3,WEDNESDAY=4,THURSDAY=5,FRIDAY=6,SATURDAY=7,SUNDAY=1)
??cal.setFirstDayOfWeek(Calendar.SATURDAY);
??// 获取当前规则下,一周的第一天
??cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
??startDate = sdf.format(cal.getTime());
??// 获取当前规则下,一周的最后一天
??cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
??endDate = sdf.format(cal.getTime());

??// 获取当前日期是一年中的第几周
??weekNum = Integer.toString(cal.get(Calendar.WEEK_OF_YEAR));

??// 将获取的数据存入Map中
??dates.put(STARTDATE, startDate);
??dates.put(ENDDATE, endDate);
??dates.put(WEEKNUM, weekNum);

??return dates;
?}

?

?/**
? * 返回一年中某一周的开始日期及结束日期
? *
? * @param year
? * @param week
? * @return
? */
?public static Map<String, String> getDaysOfWeek(Integer year, Integer week) {
??String startDate = "";
??String endDate = "";
??Map<String, String> dates = new HashMap<String, String>();

??// 获取日历对像
??Calendar cal = Calendar.getInstance();

??// 设置时间
??cal.set(cal.YEAR, year);

??// 将周六做为一周的开始(MONDAY=2,TUESDAY=3,WEDNESDAY=4,THURSDAY=5,FRIDAY=6,SATURDAY=7,SUNDAY=1)
??cal.setFirstDayOfWeek(Calendar.SATURDAY);

??// 将日历调整到第week周
??cal.set(cal.WEEK_OF_YEAR, week);

??// 调整日期到周五
??cal.set(cal.DAY_OF_WEEK, Calendar.FRIDAY);

??// 获取日期格式化对象
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

??// 格式化时间
??endDate = sdf.format(cal.getTime());

??// 将当前时间向前推移6天
??cal.add(Calendar.DATE, -6);

??// 格式化时间
??startDate = sdf.format(cal.getTime());

??// 将格式化完成的时间放入Map中
??dates.put(STARTDATE, startDate);
??dates.put(ENDDATE, endDate);

??return dates;
?}

?

?/**
? * 返回一年中某一周的开始日期及结束日期
? *
? * @param year
? * @param week
? * @return
? */
?public static Map<String, String> getDaysOfWeek(Date date, Integer week) {
??String startDate = "";
??String endDate = "";
??Map<String, String> dates = new HashMap<String, String>();

??// 获取日历对像
??Calendar cal = Calendar.getInstance();

??// 设置时间
??cal.setTime(date);

??// 将日历调整到第week周
??cal.set(cal.WEEK_OF_YEAR, week);

??// 获取日期格式化对象
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

??// 将周六做为一周的开始(MONDAY=2,TUESDAY=3,WEDNESDAY=4,THURSDAY=5,FRIDAY=6,SATURDAY=7,SUNDAY=1)
??cal.setFirstDayOfWeek(Calendar.SATURDAY);
??// 获取当前规则下,一周的第一天
??cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
??startDate = sdf.format(cal.getTime());
??// 获取当前规则下,一周的最后一天
??cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
??endDate = sdf.format(cal.getTime());

??// 将格式化完成的时间放入Map中
??dates.put(STARTDATE, startDate);
??dates.put(ENDDATE, endDate);

??return dates;
?}

?

?public static Date StrToDate(String str) {
??SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
??String[] dateStr = str.split(":", -1);
??if (dateStr.length == 2) {
???format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
??}
??if (dateStr.length == 1) {
???format = new SimpleDateFormat("yyyy-MM-dd");
??}
??Date date = null;
??try {
???date = format.parse(str);
??} catch (ParseException e) {
???e.printStackTrace();
??}
??return date;
?}

?

?public static void main(String[] args) {
??String d = getCurrentStrDate();
??System.out.println(getDaysOfWeek(d));
??System.out.println("--------------------------");
??System.out.println(getDaysOfWeek(new Date()));
??System.out.println("==========================");
??System.out.println(getDaysOfWeek(new Integer(2011), 1));
??System.out.println("--------------------------");
??System.out.println(getDaysOfWeek(new Date(), 1));
?}
}

读书人网 >编程

热点推荐