package com.linying.util;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 用于日期時(shí)間處理的工具類
* @author sitinspring
*
*/
public final class TimeUtil {
private TimeUtil() {
}
/**
* 取得當(dāng)前日期時(shí)間
*/
public static String getShortCurrDateTime() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyyMMddHHmmss");
return formatter.format(date);
}
/**
* 取得當(dāng)前日期時(shí)間
*/
public static String getCurrDateTime() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy年MM月dd日HH時(shí)mm分ss秒");
return formatter.format(date);
}
/**
* 取得當(dāng)前日期
*/
public static String getCurrentDate() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy.MM.dd");
return formatter.format(date);
}
/**
* 取得當(dāng)前年度+月份
*/
public static String getCurrentYearMonth() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy.MM");
return formatter.format(date);
}
/**
* 取得當(dāng)前日期
*/
public static String getCurrDate() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy年MM月dd日");
return formatter.format(date);
}
/**
* 取得當(dāng)前時(shí)間
*/
public static String getCurrTime() {
Date date = new Date();
Format formatter = new SimpleDateFormat("HH時(shí)mm分ss秒");
return formatter.format(date);
}
/**
* 取得當(dāng)前年月
*/
public static String getCurrMonth() {
Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy年MM月");
return formatter.format(date);
}
/**
* 在年月的基礎(chǔ)上加上或減去月份得到新年月
*/
@SuppressWarnings("deprecation")
public static String getChangedDate(String strMonth,int monthCount) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy.MM");
try {
java.util.Date date = myFormatter.parse(strMonth);
date.setMonth(date.getMonth()+monthCount);
Format formatter = new SimpleDateFormat("yyyy.MM");
return formatter.format(date);
} catch (Exception ex) {
return null;
}
}
/**
* 取得一個(gè)月中有多少天
*
* @param strMonth
* @return
*/
public static int getDaysInAMonth(String strMonth) {
String[] arr=strMonth.split("[.]");
Calendar cal = new GregorianCalendar(Integer.parseInt(arr[0]),Integer.parseInt(arr[1])-1, 1);
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return days;
}
/**
* 取得每個(gè)月的第一天是周幾
* @param strMonth
* @return
*/
public static int getWeekOfFirstDay(String strMonth){
String[] arr=strMonth.split("[.]");
Calendar xmas = new GregorianCalendar(Integer.parseInt(arr[0]),Integer.parseInt(arr[1])-1, 1);
int dayOfWeek = xmas.get(Calendar.DAY_OF_WEEK)-1;
return dayOfWeek;
}
}
posted on 2010-05-29 17:27
Ying-er 閱讀(317)
評(píng)論(0) 編輯 收藏