DateUtil 类

来源:互联网 发布:网络语吃糠是什么意思 编辑:程序博客网 时间:2024/04/30 16:14


import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 *
 * <p>
 * Title:时间转换工具
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company:
 * </p>
 *
 * @author dean
 * @version 1.0
 */
public class DateUtil {
 /**
  * 系统当前时间
  *
  * @return String 格式:YYYY-MM-DD hh:mm:ss
  */
 public static String getSysTime() {
  String data = null;
  long now = System.currentTimeMillis();
  Date d = new Date(now);
  Time t = new Time(now);
  data = d.toString() + " " + t.toString();
  return data;
 }

 /**
  * 系统当前日期 格式:YYYY-MM-DD
  *
  * @return String 格式:YYYY-MM-DD
  */
 public static String getSysDate()
 {
  long now = System.currentTimeMillis();
  Date d = new Date(now);
  return d.toString();
 }
 
 /**
  * 系统当前日期 格式:YYYY-MM-DD
  *
  * @return String 格式:YYYYMMDD
  */
 public static String getSysDateWithNoSign()
 {
  long now = System.currentTimeMillis();
  Date d = new Date(now);
  return d.toString().replaceAll("-", "");
 }
 
 public static String getSysMonthDate()
 {
  long now = System.currentTimeMillis();
  Date d = new Date(now);
  String dateTime = d.toString();
  dateTime = dateTime.substring(0, dateTime.lastIndexOf("-"));
  return dateTime;
 }

 /**
  * 时间格式转换 long->String
  *
  * @return String
  * @param date
  *            long 格式:YYYY-MM-DD hh:mm:ss
  */

 public static String formatTime(long date) {
  String time = "";
  if (date < 1000) {
   time = getSysTime();
  } else {
   Date d = new Date(date);
   Time t = new Time(date);
   time = d.toString() + " " + t.toString();

  }
  return time;
 }

 /**
  * 日期格式转换 long->String
  *
  * @param date
  *            long
  * @return String 格式:YYYY-MM-DD
  */
 public static String formatDate(long date) {
  String time = "";
  if (date < 1000) {
   time = getSysTime();
  } else {
   Date d = new Date(date);

   time = d.toString();

  }
  return time;
 }

 /**
  * 日期格式转换 String(yyyy-MM-dd�?->long
  *
  * @return long
  */

 public static long DateFormat(String data) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  java.util.Date applyDate = null;
  try {
   applyDate = sdf.parse(data);
  } catch (ParseException e) {

   e.printStackTrace();
   return 0L;
  }
  return applyDate.getTime();
 }

 /**
  * 时间格式转换 String(yyyy-MM-dd HH:mm:ss�?->long
  *
  * @param time
  *            String
  * @return long
  */
 public static long TimeFormat(String time) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  java.util.Date applyDate = null;
  try {
   applyDate = sdf.parse(time);
  } catch (ParseException e) {

   e.printStackTrace();
   return 0L;
  }
  return applyDate.getTime();
 }

 /**
  * 将Date类型转换为YYYY-MM-DD的字符串
  *
  * @param cal
  *            Date
  * @return String
  */
 public static String getDateString(java.util.Date cal) {
  if (cal != null) {
   SimpleDateFormat dateFormatter = new SimpleDateFormat(
     "yyyy-MM-dd hh:mm:ss");
   return dateFormatter.format(cal);
  } else
   return "";
 }

 /**
  * getTime - 得到日历型数据的"HH:MM:SS"样式的字符串
  *
  *
  */
 public static String getTime(java.util.Calendar cal) {
  if (cal != null) {
   SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss");
   java.util.Date theDate = cal.getTime();
   return dateFormatter.format(theDate);
  } else
   return "";
 }

 /**
  * getDateTime - 得到日历型的 "MM-DD-YYYY , HH:MM:SS" 形式的字符串
  *
  *
  */
 public static String getDateTime(java.util.Calendar cal) {
  if (cal != null) {
   SimpleDateFormat dateFormatter = new SimpleDateFormat(
     "yyyy-MM-dd hh:mm:ss");
   java.util.Date theDate = cal.getTime();
   return dateFormatter.format(theDate);
  } else
   return "";
 }

 /*
  * function - 由calendar 得到�?
  *
  *
  *
  *
  */

 public static String getYear(java.util.Calendar pCal) {
  Timestamp timestamp = getTimestamp(pCal);
  if (timestamp != null) {
   String dateString = timestamp.toString();
   String strYear = dateString.substring(0, 4);
   return strYear;
  } else {
   return "";
  }
 }

 /*
  * function - 由calendar 得到�?
  *
  *
  *
  *
  */

 public static String getMonth(java.util.Calendar pCal) {
  Timestamp timestamp = getTimestamp(pCal);
  if (timestamp != null) {
   String dateString = timestamp.toString();
   String strMonth = dateString.substring(5, 7);
   if (strMonth.substring(0, 1).equals("0")) {
    strMonth = strMonth.substring(1);
   }
   return strMonth;
  } else {
   return "";
  }
 }

 public static String getStrMonth(java.util.Calendar pCal) {
  Timestamp timestamp = getTimestamp(pCal);
  if (timestamp != null) {
   String dateString = timestamp.toString();
   String strMonth = dateString.substring(5, 7);
   return strMonth;
  } else {
   return "";
  }
 }

 /**
  * function - 由calendar 得到�?
  */

 public static String getDay(java.util.Calendar pCal) {
  Timestamp timestamp = getTimestamp(pCal);
  if (timestamp != null) {
   String dateString = timestamp.toString();
   String strDay = dateString.substring(8, 10);
   if (strDay.substring(0, 1).equals("0")) {
    strDay = strDay.substring(1);
   }
   return strDay;
  } else {
   return "";
  }
 }

 /**
  * 取得Calendar
  *
  * @param pCal
  *            Calendar
  * @return String
  */
 public static String getStrDay(java.util.Calendar pCal) {
  Timestamp timestamp = getTimestamp(pCal);
  if (timestamp != null) {
   String dateString = timestamp.toString();
   String strDay = dateString.substring(8, 10);
   return strDay;
  } else {
   return "";
  }
 }

 /**
  * function :translate Calemdar into Timestamp
  *
  *
  */
 public static Timestamp getTimestamp(java.util.Calendar cal) {
  if (cal != null) {
   java.util.Date tempDate = cal.getTime();
   return new Timestamp(tempDate.getTime());
  } else {
   return null;
  }
 }

 /** 字符串整理函数,如果为空对象返回空串�? */
 public static String getFormatString(String paramStr) {
  if (paramStr == null)
   return "";
  else
   return paramStr;
 }

 /** 字符串整理函数,如果为空对象返回空串�? */
 public static String getFormatString(Integer param) {
  if (param == null)
   return "";
  else
   return param.toString();
 }

 public static Date getDateforString(String dateString) {
  Date d_date = null;
  long l_date = DateFormat(dateString);
  d_date = new Date(l_date);
  return d_date;

 }

 public static Date getTimforString(String dateString) {
  Date d_date = null;
  long l_date = TimeFormat(dateString);
  d_date = new Date(l_date);
  return d_date;

 }
 
 public static String getSysTimeString()
 {
  String data = null;
  long now = System.currentTimeMillis();
  data = String.valueOf(now);
  return data;
 }
 
 public static void main(String[] args) {
  System.out.println(DateUtil.getSysMonthDate());
  System.out.println(DateUtil.getSysDate());
 }

}