java常用的工具类

/******************************************Date START****************************************************************/

public class DateUtil {

private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

private static final String DATE_SIMPLE_FORMAT = "yyyy-MM-dd";

private DateUtil() {
}

public static String formatDate(Date date) {
return formatDate(date, DATE_FORMAT);
}
public static String formatSimpleDate(Date date) {
return formatDate(date, DATE_SIMPLE_FORMAT);
}
public static String formatDate(Date date, String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
public static Date formatDate(String stringDate) {
return formatDate(stringDate, DATE_FORMAT);
}
public static Date formatSimpleDate(String stringDate) {
return formatDate(stringDate, DATE_SIMPLE_FORMAT);
}
public static Date formatDate(String stringDate, String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = dateFormat.parse(stringDate);
} catch (ParseException e) {
//e.printStackTrace();
return null;
}
return date;
}
public static Date getGMTDate(){
Date date = null;
try {
DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
String stringDate = dateFormat.format(new Date());
DateFormat dateFormat1 = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
date = dateFormat1.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

public static String getGMTDateStr(){
String date = null;
try {
DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
date = dateFormat.format(new Date());
} catch (Exception e) {
return null;
}
return date;
}

/**
* @Title: dayForWeek
* @Description: 判断星期几 星期一到星期七
* @param date
* @return
* @throws Exception
*/
public static int dayForWeek(Date date) throws Exception {
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
/**
* @Title: dayForWeekOfDateStr
* @Description: 判断星期几 星期一到星期七
* @param date
* @return
* @throws Exception
*/
public static int dayForWeekOfDateStr(String dateStr) throws Exception {
Calendar calendar = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat(DATE_SIMPLE_FORMAT, Locale.ENGLISH);
Date date = dateFormat.parse(dateStr);
calendar.setTime(date);
int dayForWeek = 0;
if(calendar.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}

/**
* @Title: currDay
* @Description: 时间的前一周或者后一周
* @param date
* @param days
* @return
*/
public static Date currDay(Date date,int days){
days = days * 7; 
Calendar curr = Calendar.getInstance();
curr.set(Calendar.DAY_OF_MONTH,curr.get(Calendar.DAY_OF_MONTH)+days);
date = curr.getTime();
return date;
}
public static void main(String[] args) {
System.out.println(getGMTDateStr());
System.out.println(getGMTDate());
try {
System.out.println(dayForWeekOfDateStr("2014-06-15"));
System.out.println("11");
System.out.println(dayForWeek(getGMTDate()));
System.out.println(currDay(getGMTDate(),2));
System.out.println(currDay(getGMTDate(),-1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

/******************************************Date END****************************************************************/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值