读书人

JAVA各种时间部类的取得

发布时间: 2012-09-23 10:28:11 作者: rapoo

JAVA各种时间类型的取得

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.i18n.LocaleContextHolder;


public class DateUtil {

?private static Log log = LogFactory.getLog(DateUtil.class);

?private static String defaultDatePattern = null;

?private static String timePattern = "HH:mm";

?

?public static Date getSystemTime() {
??Calendar now = Calendar.getInstance();
??return now.getTime();
??
?}

?public static synchronized String getDatePattern() {
??Locale locale = LocaleContextHolder.getLocale();
??try {
???defaultDatePattern = ResourceBundle.getBundle("yyyy-MM-dd",
?????locale).getString("date.format");
??} catch (MissingResourceException mse) {
???defaultDatePattern = "yyyy-MM-dd";
??}

??return defaultDatePattern;
?}


?public static final String getDate(Date aDate) {
??SimpleDateFormat df = null;
??String returnValue = "";

??if (aDate != null) {
???df = new SimpleDateFormat(getDatePattern());
???returnValue = df.format(aDate);
??}

??return (returnValue);
?}


?public static final Date convertStringToDate(String aMask, String strDate) {
??SimpleDateFormat df = null;
??Date date = null;
??df = new SimpleDateFormat(aMask);

??if (log.isDebugEnabled()) {
???log.debug("converting '" + strDate + "' to date with mask '"
?????+ aMask + "'");
??}

??try {
???date = df.parse(strDate);
??} catch (ParseException pe) {
???log.error("ParseException: " + pe);
??}

??return (date);
?}


?public static String getTimeNow(Date theTime) {
??return getDateTime(timePattern, theTime);
?}


?public static Calendar getToday() throws ParseException {
??Date today = new Date();
??SimpleDateFormat df = new SimpleDateFormat(getDatePattern());

??// This seems like quite a hack (date -> string -> date),
??// but it works ;-)
??String todayAsString = df.format(today);
??Calendar cal = new GregorianCalendar();
??cal.setTime(convertStringToDate(todayAsString));

??return cal;
?}

?
?public static Date getMonday(Date date) {
??Calendar c = Calendar.getInstance();
??c.setTime(date);
??if (c.get(Calendar.DAY_OF_WEEK) == 1) {
???date = DateUtil.addDays(date, -7);
???c.setTime(date);
??}

??c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
??String mondayString = DateUtil.format(c.getTime(), "yyyy-MM-dd");
??mondayString = mondayString + " 00:00:00";
??return convertStringToDate("yyyy-MM-dd HH:mm:ss", mondayString);
?}

?
?public static Date getFriday(Date date) {
??Calendar c = Calendar.getInstance();
??c.setTime(date);
??c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
??String fridayString = format(c.getTime(), "yyyy-MM-dd");
??fridayString = fridayString + " 23:59:59";
??return convertStringToDate("yyyy-MM-dd HH:mm:ss", fridayString);

?}


?public static final String getDateTime(String aMask, Date aDate) {
??SimpleDateFormat df = null;
??String returnValue = "";

??if (aDate == null) {
???log.error("aDate is null!");
??} else {
???df = new SimpleDateFormat(aMask);
???returnValue = df.format(aDate);
??}

??return (returnValue);
?}


?public static final String convertDateToString(Date aDate) {
??return getDateTime(getDatePattern(), aDate);
?}

?public static Date convertStringToDate(String strDate) {
??Date aDate = null;

??try {
???if (log.isDebugEnabled()) {
????log.debug("converting date with pattern: " + getDatePattern());
???}

???aDate = convertStringToDate(getDatePattern(), strDate);
??} catch (Exception pe) {
???log.error("Could not convert '" + strDate
?????+ "' to a date, throwing exception");

??}

??return aDate;
?}

?public static int dayDiff(Date first, Date second) {

??long msPerDay = 1000 * 60 * 60 * 24;
??long diff = (first.getTime() / msPerDay)
????- (second.getTime() / msPerDay);
??
??Long convertLong = new Long(diff);
??return convertLong.intValue();
?}
?
?public static long dayDiffMilSecond(Date first, Date second) {

??long diff = first.getTime()-second.getTime();
??Long convertLong = new Long(diff);
??return convertLong;
?}

?
?public static String format(Date date, String pattern) {
??if(date==null) return "";
??SimpleDateFormat formatter = new SimpleDateFormat(pattern);
??return formatter.format(date);
?}

?static public final Date addDays(Date target, int days) {
??long msPerDay = 1000 * 60 * 60 * 24; // 一天的毫秒数
??long msTarget = target.getTime(); // 返回从一个特定的日期(1970。。)到现在经过的毫秒数。
??long msSum = msTarget + (msPerDay * days);
??Date result = new Date();
??result.setTime(msSum); // 根据毫秒设置日期
??return result;
?}
?
?public static Date beforeDays(Date target,int days)
?{
??long msPerDay = 1000 * 60 * 60 * 24; // 一天的毫秒数
??long msTarget = target.getTime(); // 返回从一个特定的日期(1970。。)到现在经过的毫秒数。
??long msSum = msTarget - (msPerDay * days);
??Date result = new Date();
??result.setTime(msSum); // 根据毫秒设置日期
??return result;
?}
???
?public static Date formatString(String time, String format){
??try{
???SimpleDateFormat formater = new SimpleDateFormat(format);
???return formater.parse(time);
??}catch(Exception e){
???return null;
??}
?}

?
?public static Date getFirstDateOfMonth(Date date){
??Calendar calendar = Calendar.getInstance();
??calendar.setTime(date);
??calendar.set(Calendar.DAY_OF_MONTH, 1);
??calendar.set(Calendar.HOUR_OF_DAY, 0);
??calendar.set(Calendar.MINUTE, 0);
??calendar.set(Calendar.SECOND, 0);
??return calendar.getTime();
?}
?
?
?public static Date getLastlyDateOfMonth(Date date){
??Calendar calendar = Calendar.getInstance();
??calendar.setTime(date);
??int month = calendar.get(Calendar.MONTH);
??calendar.set(Calendar.MONTH, month+1);
??calendar.set(Calendar.DAY_OF_MONTH, 1);
??calendar.set(Calendar.HOUR_OF_DAY, 23);
??calendar.set(Calendar.MINUTE, 59);
??calendar.set(Calendar.SECOND, 59);
??calendar.add(Calendar.DAY_OF_MONTH, -1);
??return calendar.getTime();
?}
?
?
?public static Date getFirstDateOfNextMonth(Date date){
??Calendar calendar = Calendar.getInstance();
??calendar.setTime(date);
??int month = calendar.get(Calendar.MONTH);
??calendar.set(Calendar.MONTH, month+1);
??calendar.set(Calendar.DAY_OF_MONTH, 1);
??calendar.set(Calendar.HOUR_OF_DAY, 0);
??calendar.set(Calendar.MINUTE, 0);
??calendar.set(Calendar.SECOND, 0);
??return calendar.getTime();
??
?}
?
?public static String getCurrentDayPath()
?{
??SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
??Date now = new Date();
??String filePath = sdf.format(now) + "/";
??sdf = new SimpleDateFormat("MM");
??filePath += sdf.format(now) + "/";
??sdf = new SimpleDateFormat("dd");
??filePath += sdf.format(now) + "/";
??
??return filePath;
?}

}

public static Date getLastlyDateOfMonth(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH); calendar.set(Calendar.MONTH, month+1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.add(Calendar.DAY_OF_MONTH, -1); return calendar.getTime(); }

//
干嘛要先set 1 再add -1
直接 set 0 多省事
calendar.set(Calendar.DAY_OF_MONTH, 0);


LZ没错 7 楼 liberD 2011-01-01 现在正研究时间问题,谢谢分享!明天研究一下! 8 楼 wangyu1221 2011-01-22 一直用Calendar,都是set 0,请教set 0和set 1再add -1有什么讲究吗?

读书人网 >软件架构设计

热点推荐