package com.founder.cissservice.util;
import java.text.*;
import java.util.Calendar;
import java.util.Date;
public class FormatDate {
/**
* 根据日历时间提取yyyy-mm-dd格式的时间
*
* @param Date
* @return String 2002-12-13 Format
*/
public static String toY_M_D(Date date) throws RuntimeException {
return toSimpleDateFormat(date, "yyyy-MM-dd");
}
/**
* 根据日历时间提取yyyymmdd格式的时间
*
* @param Date
* @return String 20021213 Format
*/
public static String toYMD(Date date) throws RuntimeException {
return toSimpleDateFormat(date, "yyyyMMdd");
}
public static String toCN_Y_M_D(Date date) throws RuntimeException {
return toSimpleDateFormat(date, "yyyy年MM月dd日");
}
/**
* 返回yyyy-mm-dd hh:mm:ss格式的时间
*
* @param date
* @return
*/
public static String toY_M_D_H_M_S(Date dateTime) throws RuntimeException {
return toSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm:ss");
}
public static String toCN_Y_M_D_H_M_S(Date dateTime) throws RuntimeException {
return toSimpleDateFormat(dateTime, "yyyy年MM月dd日 HH时mm分ss秒");
}
/**
* 返回yyyy-mm-dd hh:mm格式的时间
*
* @param date
* @return
*/
public static String toY_M_D_H_M(Date dateTime) throws RuntimeException {
return toSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm");
}
public static String toCN_Y_M_D_H_M(Date dateTime) throws RuntimeException {
return toSimpleDateFormat(dateTime, "yyyy年MM月dd日 HH时mm分");
}
/**
* 根据时间返回hh:mm:ss格式的字符串
*
* @param time
* @return
* @throws Exception
*/
public static String toH_M_S(Date time) throws RuntimeException {
if (time == null) {
return null;
} else {
Calendar cd = Calendar.getInstance();
cd.setTime(time);
return cd.get(Calendar.HOUR_OF_DAY)+":"+cd.get(Calendar.MINUTE)+":"+cd.get(Calendar.SECOND);
// return time.getHours() + ":" + time.getMinutes() + ":"
// + time.getSeconds();
}
}
/**
* 字符串格式转换称日期格式
*
* @param date
* @return
* @throws Exception
*/
public static Date fromY_M_D(String date) throws RuntimeException {
return fromSimpleDateFormat(date, "yyyy-MM-dd");
}
/**
* 日期时间格式
*
* @param date
* @return
* @throws Exception
*/
public static Date fromY_M_D_H_M_S(String dateTime) throws RuntimeException {
return fromSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm:ss");
}
/**
* 根据时分秒装还称时间
*
* @param time
* @return
* @throws Exception
*/
public static Date fromH_M_S(String time) throws RuntimeException {
try {
return fromSimpleDateFormat(time, "HH:mm:ss");
} catch (Exception e) {
return fromSimpleDateFormat(time, "yyyy-MM-dd HH:mm:ss");
}
}
/**
* 得到当前的时间
*
* @return
*/
public static Date getCurrDate() {
return new Date();
}
/**
* 统一的时间构造方法
*
* @param date
* @param simpleDateFormat
* @return
*/
public static String toSimpleDateFormat(Date date, String simpleDateFormat) {
SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
if (date == null) {
return null;
} else {
return format.format(date);
}
}
/**
* 简单时间格式
*
* @param date
* @param simpleDateFormat
* @return
*/
public static Date fromSimpleDateFormat(String date, String simpleDateFormat)
throws RuntimeException {
SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
if (date == null || date.equals("")) {
return null;
} else {
try {
return format.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
}