/******************************************Date START****************************************************************/
}
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();
}
}
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****************************************************************/