时间格式转换公共类1

来源:互联网 发布:informix批量生成数据 编辑:程序博客网 时间:2024/06/06 04:49

package com.ctc.cmsutil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.log4j.Logger;

public class DateUtil {

 private static Logger log = Logger.getLogger(DateUtil.class);
 public static final String format1 = "yyyy-MM-dd";
 public static final String format2 = "yyyy-MM-dd HH:mm:ss";
 public static final String format3 = "HH:mm:ss";
 public static final String format4 = "yyyy-MM-dd HH";
 public static final String format5 = "yyyyMMddHHmmss";
 public static final String format6 = "yyyy年MM月dd日";

 /**
  * 取得给定日期的凌晨0:00时间
  *
  * @return
  */
 public static Date getDateFirst(Date date) {
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  Calendar calfirstday = new GregorianCalendar(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH), ca.get(Calendar.DAY_OF_MONTH));
  return calfirstday.getTime();
 }

 /**
  * 取得给定日期相差n月的第一天凌晨0:00时间<br/>
  * 1 n可以为负值或0,分别取之前的月份或指定月份
  *
  * @param date
  * @param n
  * @return
  */
 public static Date getMonthFirst(Date date, int n) {
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  Calendar calfirstday = new GregorianCalendar(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH) + n, 1);
  return calfirstday.getTime();
 }

 /**
  * 取得当月第一天凌晨0:00时间
  *
  * @return
  */
 public static Date getThisMonthFirst() {
  Calendar cal = Calendar.getInstance();
  Calendar calfirstday = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
  return calfirstday.getTime();
 }

 /**
  * 取得下一天的凌晨0:10时间
  *
  * @return
  */
 public static Date getNextDay() {
  Calendar cal = Calendar.getInstance();
  Calendar calnextday = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) + 1);
  calnextday.add(Calendar.MINUTE, 10);
  return calnextday.getTime();
 }

 /**
  * 取得今天凌晨0:10时间
  *
  * @return
  */
 public static Date getToday() {
  Calendar cal = Calendar.getInstance();
  Calendar calnextday = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
  calnextday.add(Calendar.MINUTE, 10);
  return calnextday.getTime();
 }

 /**
  * 取得下一天的时间字符串
  *
  * @return
  */
 public static String getNextDay(String date) {

  SimpleDateFormat sdf = new SimpleDateFormat(format1);
  Calendar cal = Calendar.getInstance();
  try {
   cal.setTime(sdf.parse(date));
  } catch (ParseException e) {
   log.error("Exception:", e);
  }
  cal.add(Calendar.DAY_OF_MONTH, 1);
  return sdf.format(cal.getTime());
 }

 public static Date getNextDay(Date date) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  cal.add(Calendar.DAY_OF_MONTH, 1);
  return cal.getTime();
 }

 /**
  * 字符串转化成日期
  *
  * @param date
  * @return
  */
 public static Date convert(String date) {
  Date retValue = null;
  SimpleDateFormat sdf = new SimpleDateFormat(format1);
  try {
   retValue = sdf.parse(date);
  } catch (ParseException e) {
   log.error("", e);
  }
  return retValue;
 }

 public static Date convert(String date, String format) {
  Date retValue = null;
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  try {
   retValue = sdf.parse(date);
  } catch (ParseException e) {
   log.error("字符串转日期失败", e);
  }
  return retValue;
 }

 public static Date convert1(String date) {
  Date retValue = null;
  SimpleDateFormat sdf = new SimpleDateFormat(format2);
  try {
   retValue = sdf.parse(date);
  } catch (ParseException e) {
   log.error("", e);
  }
  return retValue;
 }

 public static Date convert2(String date) {
  Date retValue = null;
  SimpleDateFormat sdf = new SimpleDateFormat(format3);
  try {
   retValue = sdf.parse(date);
  } catch (ParseException e) {
   log.error("", e);
  }
  return retValue;
 }

 public static Date convert3(String date) {
  Date retValue = null;
  SimpleDateFormat sdf = new SimpleDateFormat(format4);
  try {
   retValue = sdf.parse(date);
  } catch (ParseException e) {
   log.error("", e);
  }
  return retValue;
 }

 /**
  * 日期转化成字符串
  *
  * @param date
  * @param format
  * @return
  */
 public static String convert(Date date, String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  String retstr = sdf.format(date);
  return retstr;
 }

 /**
  * 取得与今天相隔plus天凌晨0:00时间
  *
  * @return
  */
 public static Date getDateFromToday(int plus) {

  Calendar cal = Calendar.getInstance();
  Calendar calnextday = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH) + plus);
  return calnextday.getTime();
 }

 public static Date getDateFromTodayByHour(int plus) {

  Calendar cal = Calendar.getInstance();
  Calendar calnextday = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal
    .get(Calendar.HOUR_OF_DAY)
    + plus, 0);
  return calnextday.getTime();
 }

 public static String getTableSuffix(Date date) {

  Calendar cNextMonth = Calendar.getInstance();
  cNextMonth.setTime(date);
  String year = String.valueOf(cNextMonth.get(Calendar.YEAR)).substring(2);
  String month = String.valueOf(cNextMonth.get(Calendar.MONTH) + 1);
  if (month.length() == 1)
   month = "0" + month;
  return year + month;

 }

 /**
  * 取得list中比默认值小的最小date,null认为最大,全比默认值大,返回默认值
  *
  * @param dateList
  * @param defaultDate
  * @return
  */
 public static Date getMinDateByList(List<Date> dateList, Date defaultDate) {
  Date tempDate = defaultDate;
  for (int i = 0; i < dateList.size(); i++) {
   if (dateList.get(i) == null)
    continue;
   if (dateList.get(i).getTime() < tempDate.getTime())
    tempDate = dateList.get(i);
  }
  return tempDate;
 }

 /**
  * 两个日期相差天数
  *
  * @param date1
  * @param date2
  * @return
  */
 public static int getDiffDays(Date date1, Date date2) {
  if ((date1.getTime() - date2.getTime()) % (1000 * 60 * 60 * 24) == 0)
   return (int) (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24);
  else
   return (int) (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24) - 1;
 }

 /** 计算两个日期之间的天数 */
 public static int daysofTwo(Date early, Date late) {
  int count = 0;
  if (early == null && late == null)
   return count;
  Calendar calst = Calendar.getInstance();
  Calendar caled = Calendar.getInstance();
  calst.setTime(early);
  caled.setTime(late);
  // 设置时间为0时
  calst.set(Calendar.HOUR_OF_DAY, 0);
  calst.set(Calendar.MINUTE, 0);
  calst.set(Calendar.SECOND, 0);
  caled.set(Calendar.HOUR_OF_DAY, 0);
  caled.set(Calendar.MINUTE, 0);
  caled.set(Calendar.SECOND, 0);
  // 得到两个日期相差的天数
  int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
  return days;
 }

 /** 返回最小日期 */
 public static Date MaxDate(Date date1, Date date2) {
  if (date1 == null || date2 == null)
   return null;
  Calendar ca = Calendar.getInstance();
  ca.setTime(date1);
  int day1 = ca.get(Calendar.DAY_OF_YEAR);
  ca.setTime(date2);
  int day2 = ca.get(Calendar.DAY_OF_YEAR);
  return day1 > day2 ? date2 : date1;
 }

 /** 比较日期大小,如果有个日期为空。返回false */
 public static boolean maxDate(Date d1, Date d2) {
  if (d1 != null && d2 != null) {
   Calendar ca = Calendar.getInstance();
   ca.setTime(d1);
   int day1 = ca.get(Calendar.DAY_OF_YEAR);
   ca.setTime(d2);
   int day2 = ca.get(Calendar.DAY_OF_YEAR);
   if (day1 > day2) {
    return true;
   }
  }
  return false;
 }

 /** 在当前日期上加多少天 */
 public static Date addDateDays(Date date, int days) {
  if (date == null)
   return null;
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  ca.add(Calendar.DAY_OF_YEAR, days);
  return ca.getTime();
 }

 /** 在当前日期上加多少小时 */
 public static Date addDateHours(Date date, int hours) {
  if (date == null)
   return null;
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  ca.add(Calendar.HOUR_OF_DAY, hours);
  return ca.getTime();
 }

 /** 在当前日期上加多少分钟 */
 public static Date addDateMinutes(Date date, int minutes) {
  if (date == null)
   return null;
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  ca.add(Calendar.MINUTE, minutes);
  return ca.getTime();
 }

 public static boolean isRightDate(String s, String formatType) {
  boolean b = true;
  SimpleDateFormat simpledateformat = new SimpleDateFormat(formatType);
  try {
   simpledateformat.parseObject(s);
  } catch (Exception ex) {
   b = false;
  }
  return b;
 }

 public static String NowDateTime(String format) {
  String s = "";
  try {
   Date date = new Date();
   SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
   s = simpledateformat.format(date);
  } catch (Exception ex) {
   log.error("", ex);
  }
  return s;
 }

 /**
  * 获取当前日期是星期几<br>
  *
  * @param dt
  * @return 当前日期是星期几
  */
 public static String WeekOfDate(Date dt, boolean isS) {
  String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  Calendar cal = Calendar.getInstance();
  cal.setTime(dt);
  int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  if (w < 0)
   w = 0;
  if (isS)
   return weekDays[w];
  return w + "";
 }

 /**
  * 获取当前小时
  *
  * @param dt
  * @return int
  * */
 public static int hourOfDate(Date dt) {
  int hour = -1;
  if (dt != null) {
   Calendar cal = Calendar.getInstance();
   cal.setTime(dt);
   hour = cal.get(Calendar.HOUR_OF_DAY);
  }
  return hour;
 }

 /**
  * 获取当前分钟
  *
  * @param dt
  * @return int
  * */
 public static int minuteOfDate(Date dt) {
  int minute = -1;
  if (dt != null) {
   Calendar cal = Calendar.getInstance();
   cal.setTime(dt);
   minute = cal.get(Calendar.MINUTE);
  }
  return minute;
 }

 /**
  * 获取当前秒钟
  *
  * @param dt
  * @return int
  *
  * */
 public static int secondOfDate(Date dt) {
  int second = -1;
  if (dt != null) {
   Calendar cal = Calendar.getInstance();
   cal.setTime(dt);
   second = cal.get(Calendar.SECOND);
  }
  return second;
 }

 /**
  * 返回当前时间是这一年中第几周
  *
  * @param dt
  * @return
  */
 public static int weekOfYear(Date dt) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(dt);
  int w = cal.get(Calendar.WEEK_OF_YEAR);
  return w;
 }

 public static boolean isBetween(Date compareDate, Date startDate, Date endDate) {
  return (compareDate.compareTo(startDate) >= 0 && compareDate.compareTo(endDate) <= 0);
 }

 public static void main(String[] args) {
  System.out.println(minuteOfDate(new Date()));
 }
}

原创粉丝点击