日期格式转换工具类

来源:互联网 发布:iphonep图软件推荐 编辑:程序博客网 时间:2024/05/20 02:53

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date Util.
 */
public class DateUtil {
 /**
  * Return current datetime string.
  *
  * @return current datetime, pattern: "yyyy-MM-dd HH:mm:ss".
  */
 public static String getDateTime() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dt = sdf.format(new Date());
  return dt;
 }

 /**
  * Return current datetime string.
  *
  * @param pattern
  *            format pattern
  * @return current datetime
  */
 public static String getDateTime(String pattern) {
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  String dt = sdf.format(new Date());
  return dt;
 }

 /**
  * Return the date string.
  *
  * @param dateStr
  *            the date string
  * @param pattern
  *            format pattern
  * @return String the date string
  */
 public static String getDateTime(String dateStr, String pattern) {
  if (dateStr.equals("") || dateStr == null)
   return "";

  Date date = parseShort(dateStr); // 转化为日期
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  String dt = sdf.format(date);
  return dt;
 }

 /**
  * 根据日期字符串和模式分离出年,月,日。
  *
  * @param dateStr
  *            日期字符串
  * @param pattern
  *            日期格式化字符串(yyyy-MM-dd)
  * @return int[] 返回月,日,年的数组
  */
 public static int[] getDate(String dateStr, String pattern) {
  int dateTime[] = new int[3];
  Date date = parseShort(dateStr); // 转化为日期
  Calendar cale = Calendar.getInstance();
  cale.setTime(date);
  dateTime[0] = cale.get(Calendar.YEAR);
  dateTime[1] = cale.get(Calendar.MONTH) + 1;
  dateTime[2] = cale.get(Calendar.DATE);

  return dateTime;
 }

 /**
  * Return short format datetime string.
  *
  * @param date
  *            java.util.Date
  * @return short format datetime
  */
 public static String shortFmt(Date date) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
  return sdf.format(date);
 }

 /**
  * Parse a datetime string.
  *
  * @param param
  *            datetime string, pattern: "yyyy-MM-dd HH:mm:ss".
  * @return java.util.Date
  */
 public static Date parse(String param) {
  Date date = null;
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  try {
   date = sdf.parse(param);
  } catch (ParseException ex) {
   date = new Date();
  }
  return date;
 }
 /**
  * 根据给定日期,取得年份
  * @param date
  * @return
  */
 public static String getYear(Date date) {
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String year = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  try {
   year = sdf.format(date);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return year;
 }

 /**
  * 根据给定日期,取得月
  * @param date
  * @return
  */
 public static String getMonth(Date date) {
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String month = "";
  SimpleDateFormat sdf = new SimpleDateFormat("MM");
  try {
   month = sdf.format(date);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return month;
 }
 /**
  * 根据给定日期,取得日期号
  * @param date
  * @return
  */
 public static String getDay(Date date) {
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String day = "";
  SimpleDateFormat sdf = new SimpleDateFormat("dd");
  try {
   day = sdf.format(date);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return day;
 }
 /**
  * 根据给定日期,取得小时
  * @param date
  * @return
  */
 public static String getHour(Date date) {
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String hour = "";
  SimpleDateFormat sdf = new SimpleDateFormat("HH");
  try {
   hour = sdf.format(date);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return hour;
 }
 /**
  * 根据给定日期,取得分钟
  * @param date
  * @return
  */
 public static String getMinute(Date date) {
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String minute = "";
  SimpleDateFormat sdf = new SimpleDateFormat("mm");
  try {
   minute = sdf.format(date);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return minute;
 }
 /**
  * 根据给定日期,取得秒数
  * @param date
  * @return
  */
 public static String getSeconds(Date date) {
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String seconds = "";
  SimpleDateFormat sdf = new SimpleDateFormat("ss");
  try {
   seconds = sdf.format(date);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return seconds;
 }
 /**
  * Parse a datetime string to shorttime format.
  *
  * @param param
  *            datetime string, pattern: "yyyy-MM-dd".
  * @return java.util.Date
  */
 public static Date parseShort(String param) {
  return parse(param, "yyyy-MM-dd");
 }

 /**
  * 根据模式返回日期字符串的Date对象。
  *
  * @param param
  *            日期字符串,如2008-12-12 23:10:10
  * @param pattern
  *            日期格式,如yyyy-MM-dd
  * @return
  */
 public static Date parse(String param, String pattern) {
  Date date = null;
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  try {
   date = sdf.parse(param);
  } catch (ParseException ex) {
   ex.printStackTrace();
   return new Date();
  } catch (Exception ex) {
   ex.printStackTrace();
   return new Date();
  }
  return date;
 }

 /**
  * 把给定的日期字符串,以pattern格式转换为日期类型。
  *
  * @param dateStr
  *            日期字符串
  * @param pattern
  *            格式化字符串,如"yyyy-MM-dd HH:mm:ss".
  * @return Date 返回Date型日期。
  */
 public static Date parseDate(String dateStr, String pattern) {
  Date date = null;
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  try {
   date = sdf.parse(dateStr);
  } catch (ParseException ex) {
   date = new Date();
  }

  return date;
 }

 /**
  * 把给定的日期字符串,以"yyyy-MM-dd HH:mm:ss" 格式转换为日期类型。
  *
  * @param dateStr
  *            日期字符串
  * @return Date 返回Date型日期。
  */
 public static Date parseDate(String dateStr) {
  return parseDate(dateStr, "yyyy-MM-dd HH:mm:ss");
 }

 /**
  * 把给定的日期字符串,自定义格式转换为日期类型。
  *
  * @param str
  *            日期字符串
  * @param format
  *            格式
  * @return Date 返回Date型日期。
  * @author liguocai
  */
 public static Date toDateByFormat(String str, String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  try {
   return sdf.parse(str);
  } catch (ParseException e) {
   e.printStackTrace();
   return null;
  }
 }

 /**
  * 把给定的日期字符串,自定义格式转换为字符串类型。
  *
  * @param date
  *            日期
  * @param format
  *            格式
  * @return String 返回String型日期。
  * @author liguocai
  */
 public static String toStringByFormat(Date date, String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  String tmpStr = "";
  if (date != null) {
   tmpStr = sdf.format(date);
  }

  return tmpStr;
 }
}

原创粉丝点击