欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java针对于时间转换的DateUtils工具类

程序员文章站 2024-02-24 10:10:52
本文实例为大家分享了时间转换的dateutils工具类,供大家参考,具体内容如下 import java.sql.timestamp; import java....

本文实例为大家分享了时间转换的dateutils工具类,供大家参考,具体内容如下

import java.sql.timestamp;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.hashmap;
import java.util.map;
import net.sf.json.jsonobject;

/**
 * 时间日期工具类
 * 
 * @author wul
 * 2015-12-31
 */
public class dateutil {

  public static final string date_normal_format = "yyyy-mm-dd"; 
  public static final string datetime_normal_format = "yyyy-mm-dd hh:mm:ss"; 
  public static final string date_compact_format = "yyyymmdd"; 
  public static final string datetime_compact_format = "yyyymmddhhmmss"; 
  public static final string ym_normal_format = "yyyy-mm"; 
  public static final string ym_compact_format = "yyyymm"; 

  /**
   * string转timestamp
   * @param datestr
   * @return
   * @author wul
   * 2016-1-17
   */
  public static timestamp stringtotimestamp(string datestr) {
    try { 
      if(datestr.length() <= 10) {
        datestr += " 00:00:00";
      }
      return timestamp.valueof(datestr); 
    } catch (exception e) { 
      e.printstacktrace(); 
      return null;
    } 
  }

  /**
   * string转date
   * @param datestr
   * @param format
   * @return
   * @author wul
   * 2016-1-17
   */
  public static date stringtodate(string datestr, string format) {
    if(datestr == null || "".equals(datestr)){
      return null;
    }
    date date = null; 
    //注意format的格式要与日期string的格式相匹配 
    simpledateformat sdf = new simpledateformat(format); 
    try { 
      date = sdf.parse(datestr); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
    return date;
  }

  /**
   * date转string
   * @param date
   * @param format
   * @return
   * @author wul
   * 2016-1-17
   */
  public static string datetostring(date date, string format) {
    simpledateformat sdf = new simpledateformat(format);
    string currentdate = sdf.format(date);
    return currentdate;
  }

  /**
   * date转timestamp
   * @param date
   * @return
   * @author wul
   * 2016-1-17
   */
  public static timestamp datetotimestamp(date date) {
    timestamp ts = new timestamp(date.gettime());
    return ts;
  }

  /**
   * timestamp转string
   * @param ts
   * @return
   * @author wul
   * 2016-1-17
   */
  public static string timestamptostring(timestamp ts) {
    string tsstr = null; 
    simpledateformat sdf = new simpledateformat(datetime_normal_format); 
    try { 
      tsstr = sdf.format(ts); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
    return tsstr;
  }

  /**
   * timestamp转date
   * @param ts
   * @return
   * @author wul
   * 2016-1-17
   */
  public static date timestamptodate(timestamp ts) {
    return ts;
  }

  /**
   * 获得当前时间并格式化:yyyy-mm-dd hh:mm:ss
   * @return
   */
  public static string getcurrenttimenormal() {

    simpledateformat sdf = new simpledateformat(datetime_normal_format);
    string currentdate = sdf.format(new date());
    return currentdate;
  }
  /**
   * 获得当前时间并格式化:yyyymmddhhmmss
   * @return
   */
  public static string getcurrenttimecompact() {

    simpledateformat sdf = new simpledateformat(datetime_compact_format);
    string currentdate = sdf.format(new date());
    return currentdate;
  }

  /**
   * 获得当前时间并格式化:yyyy-mm-dd
   * @return
   */
  public static string getcurrentdatenormal() {

    simpledateformat sdf = new simpledateformat(date_normal_format);
    string currentdate = sdf.format(new date());
    return currentdate;
  }

  /**
   * 获得当前时间并格式化:yyyymmdd
   * @return
   */
  public static string getcurrentdatecompact() {

    simpledateformat sdf = new simpledateformat(date_compact_format);
    string currentdate = sdf.format(new date());
    return currentdate;
  }

  /**
   * 将20101202时间格式化为2010-12-02
   * 
   * @param datestring 时间格式:yyyymmdd
   * @return
   */
  public static string getdatecompacttonormal(string datestring){

    stringbuilder sb = new stringbuilder();
    sb.append(datestring.substring(0,4)).append("-").append(datestring.subsequence(4, 6)).append("-").append(datestring.substring(6, 8));
    return sb.tostring();
  }

  /**
   * 将20101202101423时间格式化为2010-12-02 10:14:23
   * 
   * @param datestring 时间格式:yyyymmddhhmmss
   * @return
   */
  public static string getdatetimecompacttonormal(string datestring){

    stringbuilder sb = new stringbuilder();
    sb.append(datestring.substring(0,4)).append("-").append(datestring.subsequence(4, 6)).append("-").append(datestring.substring(6, 8))
        .append(" ").append(datestring.substring(8, 10)).append(":").append(datestring.substring(10, 12)).append(":").append(datestring.substring(12));
    return sb.tostring();
  }

  /**
   * 把界面输入的时间转为间凑的时间字符串
   * 将2010-12-02 10:14:23时间格式化为20101202101423
   * @param datenormalstr string
   * @return string
   */
  public static string getcompactstring(string datenormalstr) {
    stringbuffer ret = new stringbuffer();
    try {
      ret.append(datenormalstr.substring(0, 4));
      ret.append(datenormalstr.substring(5, 7));
      ret.append(datenormalstr.substring(8, 10));
      ret.append(datenormalstr.substring(11, 13));
      ret.append(datenormalstr.substring(14, 16));
      ret.append(datenormalstr.substring(17, 19));
    } catch (exception ex) {
      // 如果字串不够长度,则返回前面的部分
    }
    return ret.tostring();
  }
  /**
   * 将20101202(101423)时间格式 获得年份
   * @param datestring  时间格式:yyyymmdd(hhmmss)
   * @return
   */
  public static string getyear(string datestring){

    return datestring.substring(0,4);
  }

  /**
   * 将20101202(101423)时间格式 获得月份
   * @param datestring  时间格式:yyyymmdd(hhmmss)
   * @return
   */
  public static string getmonth(string datestring){

    return datestring.substring(4,6);
  }

  /**
   * 将20101202时间格式 获得日期
   * @param datestring  时间格式:yyyymmdd
   * @return
   */
  public static string getdaynotime(string datestring){
    return datestring.substring(6);
  }

  /**
  * 获取当前日期之前的日期,按天数向前推
  * @param numval
  * @param dateformat
  * @return
  * @author wul
  * 2016-1-17
  */
  public static string getbeforedateplusday(int numval, string dateformat) {
    calendar calendar = calendar.getinstance();
    long currenttimemillis = calendar.gettimeinmillis();

    long hourinmillis = 60 * 60 * 1000;
    long dval = numval * 24 * hourinmillis;

    simpledateformat sdf = new simpledateformat(dateformat);
    string currentdate = sdf.format(currenttimemillis - dval);
    return currentdate;
  }

  /**
   * 获取当前日期之前的日期,按天数向前推
   * @param numval
   * @param dateformat
   * @return
   * @author wul
   * 2016-1-17
   */
  public static string getafterdateplusday(int numval, string dateformat) {
    calendar calendar = calendar.getinstance();
    long currenttimemillis = calendar.gettimeinmillis();

    long hourinmillis = 60 * 60 * 1000;
    long dval = numval * 24 * hourinmillis;

    simpledateformat sdf = new simpledateformat(dateformat);
    string currentdate = sdf.format(currenttimemillis + dval);
    return currentdate;
  }

  /**
   * 获取当前日期之前的日期,按小时向前推
   * @param numval
   * @param dateformat
   * @return
   * @author wul
   * 2016-1-17
   */
  public static string getbeforedateplushour(int numval, string dateformat) {
    calendar calendar = calendar.getinstance();
    long currenttimemillis = calendar.gettimeinmillis();

    long hourinmillis = 60 * 60 * 1000;
    long dval = numval * hourinmillis;

    simpledateformat sdf = new simpledateformat(dateformat);
    string currentdate = sdf.format(currenttimemillis - dval);
    return currentdate;
  }

  /**
   * 获取当前日期之前的日期,按小时向前推
   * @param numval
   * @param dateformat
   * @return
   * @author wul
   * 2016-1-17
   */
  public static string getafterdateplushour(int numval, string dateformat) {
    calendar calendar = calendar.getinstance();
    long currenttimemillis = calendar.gettimeinmillis();

    long hourinmillis = 60 * 60 * 1000;
    long dval = numval * hourinmillis;

    simpledateformat sdf = new simpledateformat(dateformat);
    string currentdate = sdf.format(currenttimemillis + dval);
    return currentdate;
  }

  /**
   * 两个日期相差天数
   * @param begindate
   * @param enddate
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int daysbetween(date begindate, date enddate) {
    calendar cal = calendar.getinstance();
    cal.settime(begindate);
    long time1 = cal.gettimeinmillis();
    cal.settime(enddate);
    long time2 = cal.gettimeinmillis();
    long between_days = (time2 - time1) / (1000 * 3600 * 24);
    return integer.parseint(string.valueof(between_days));
  }

  /**
   * 获取某月天数
   * @param year
   * @param month
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getmonthdays(int year, int month) {
    calendar cal = calendar.getinstance();
    cal.set(calendar.year, year);
    cal.set(calendar.month, month - 1);
    return cal.getactualmaximum(calendar.date);
  }

  /**
   * 给时间加减年份
   * @param date
   * @param plustime
   * @return
   * @author wul
   * 2016-1-18
   */
  public static date getdateplusyear(date date, int plustime) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    cal.add(calendar.year, plustime);
    date d = cal.gettime();
    return d;
  }

  /**
   * 给时间加减月份
   * @param date
   * @param plustime
   * @return
   * @author wul
   * 2016-1-18
   */
  public static date getdateplusmonth(date date, int plustime) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    cal.add(calendar.month, plustime);
    date d = cal.gettime();
    return d;
  }

  /**
   * 给时间加减天数
   * @param date
   * @param plustime
   * @return
   * @author wul
   * 2016-1-18
   */
  public static date getdateplusday(date date, int plustime) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    cal.add(calendar.date, plustime);
    date d = cal.gettime();
    return d;
  }

  /**
   * 给时间加减小时
   * @param date
   * @param plustime
   * @return
   * @author wul
   * 2016-1-18
   */
  public static date getdateplushour(date date, int plustime) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    cal.add(calendar.hour, plustime);
    date d = cal.gettime();
    return d;
  }

  /**
   * 给时间加减分钟
   * @param date
   * @param plustime
   * @return
   * @author wul
   * 2016-1-18
   */
  public static date getdateplusminute(date date, int plustime) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    cal.add(calendar.minute, plustime);
    date d = cal.gettime();
    return d;
  }

  /**
   * 给时间加减秒
   * @param date
   * @param plustime
   * @return
   * @author wul
   * 2016-1-18
   */
  public static date getdateplussecond(date date, int plustime) {
    calendar cal = calendar.getinstance();
    cal.settime(date);
    cal.add(calendar.second, plustime);
    date d = cal.gettime();
    return d;
  }

  /**
   * 返回当前年
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getcurrentyear() {
    calendar calendar = calendar.getinstance();
    return calendar.get(1);
  }

  /**
   * 返回当前月
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getcurrentmonth() {
    calendar calendar = calendar.getinstance();
    return calendar.get(2) + 1;
  }

  /**
   * 返回当前天
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getcurrentday() {
    calendar calendar = calendar.getinstance();
    return calendar.get(5);
  }

  /**
   * 返回当前小时
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getcurrenthour() {
    calendar calendar = calendar.getinstance();
    return calendar.get(11);
  }

  /**
   * 返回当前分钟
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getcurrentminute() {
    calendar calendar = calendar.getinstance();
    return calendar.get(12);
  }

  /**
   * 返回当前秒
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getcurrentsecond() {
    calendar calendar = calendar.getinstance();
    return calendar.get(13);
  }

  /**
   * 返回当前年
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getyear(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    return calendar.get(1);
  }

  /**
   * 返回当前月
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getmonth(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    return calendar.get(2) + 1;
  }

  /**
   * 返回当前天
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getday(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    return calendar.get(5);
  }

  /**
   * 返回当前小时
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int gethour(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    return calendar.get(11);
  }

  /**
   * 返回当前分钟
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getminute(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    return calendar.get(12);
  }

  /**
   * 返回当前秒
   * @return
   * @author wul
   * 2016-1-18
   */
  public static int getsecond(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    return calendar.get(13);
  }

  public static void main(string[] args) {
    system.out.println(dateutil.datetostring(new java.sql.date(system.currenttimemillis()), dateutil.date_normal_format));
    map<string,object> map = new hashmap<string,object>();
    map.put("date", new date());
    string json = jsonobject.fromobject(map).tostring();
    system.out.println(json);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。