最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java針對于時間轉換的DateUtils工具類

 更新時間:2017年12月08日 08:53:33   作者:lettle_redhat  
這篇文章主要為大家詳細介紹了java針對于時間轉換的DateUtils工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了時間轉換的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);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java?Mybatis?foreach嵌套foreach?List<list<Object>>問題

    Java?Mybatis?foreach嵌套foreach?List<list<Object>&

    在MyBatis的mapper.xml文件中,foreach元素常用于動態(tài)生成SQL查詢條件,此元素包括item(必選,元素別名)、index(可選,元素序號或鍵)、collection(必選,指定迭代對象)、open、separator、close(均為可選,用于定義SQL結構)
    2024-09-09
  • Java使用synchronized修飾方法來同步線程的實例演示

    Java使用synchronized修飾方法來同步線程的實例演示

    synchronized下的方法控制多線程程序中的線程同步非常方便,這里就來看一下Java使用synchronized修飾方法來同步線程的實例演示,需要的朋友可以參考下
    2016-06-06
  • SpringBoot中多環(huán)境配置和@Profile注解示例詳解

    SpringBoot中多環(huán)境配置和@Profile注解示例詳解

    這篇文章主要介紹了SpringBoot中多環(huán)境配置和@Profile注解,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • SpringBoot測試之@SpringBootTest與MockMvc的實戰(zhàn)應用小結

    SpringBoot測試之@SpringBootTest與MockMvc的實戰(zhàn)應用小結

    本文將深入探討SpringBoot測試中兩個核心工具:@SpringBootTest注解與MockMvc測試框架的實戰(zhàn)應用,幫助開發(fā)者構建更穩(wěn)健的測試體系,提高代碼質量與可維護性,感興趣的朋友一起看看吧
    2025-03-03
  • Java通過調用C/C++實現的DLL動態(tài)庫——JNI的方法

    Java通過調用C/C++實現的DLL動態(tài)庫——JNI的方法

    這篇文章主要介紹了Java通過調用C/C++實現的DLL動態(tài)庫——JNI的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Springboot整合Freemarker的實現詳細過程

    Springboot整合Freemarker的實現詳細過程

    這篇文章主要介紹了Springboot整合Freemarker的實現詳細過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 關于mybatis plus 中的查詢優(yōu)化問題

    關于mybatis plus 中的查詢優(yōu)化問題

    這篇文章主要介紹了關于mybatis plus 中的查詢優(yōu)化問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • java實現用戶自動登錄

    java實現用戶自動登錄

    這篇文章主要為大家詳細介紹了java用戶自動登錄的實現方法,分為六個步驟實現用戶自動登錄,并驗證用戶是否已經登錄,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Java實現復原IP地址的方法

    Java實現復原IP地址的方法

    這篇文章主要介紹了Java實現復原IP地址的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • java 實現DES 加密解密的示例

    java 實現DES 加密解密的示例

    這篇文章主要介紹了java 實現DES 加密解密的示例代碼,幫助大家更好的理解和使用Java進行加解密,感興趣的朋友可以了解下
    2020-12-12

最新評論

兴海县| 彩票| 博野县| 武义县| 桐梓县| 苗栗县| 确山县| 育儿| 西林县| 西贡区| 肇州县| 丹棱县| 定边县| 苏尼特左旗| 肇东市| 章丘市| 钦州市| 清新县| 蓝田县| 乌鲁木齐市| 鄂托克前旗| 洪江市| 铁岭县| 鄯善县| 和顺县| 克拉玛依市| 同德县| 上高县| 洪江市| 湖北省| 大城县| 临洮县| 饶平县| 鹿泉市| 雅安市| 丹东市| 荆门市| 昌都县| 景宁| 湖北省| 桂阳县|