栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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"; 

  
  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;
    } 
  }

  
  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;
  }

  
  public static String dateToString(Date date, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    String currentDate = sdf.format(date);
    return currentDate;
  }

  
  public static Timestamp dateToTimestamp(Date date) {
    Timestamp ts = new Timestamp(date.getTime());
    return ts;
  }

  
  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;
  }

  
  public static Date timestampToDate(Timestamp ts) {
    return ts;
  }

  
  public static String getCurrentTimeNormal() {

    SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_NORMAL_FORMAT);
    String currentDate = sdf.format(new Date());
    return currentDate;
  }
  
  public static String getCurrentTimeCompact() {

    SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_COMPACT_FORMAT);
    String currentDate = sdf.format(new Date());
    return currentDate;
  }

  
  public static String getCurrentDateNormal() {

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_NORMAL_FORMAT);
    String currentDate = sdf.format(new Date());
    return currentDate;
  }

  
  public static String getCurrentDateCompact() {

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_COMPACT_FORMAT);
    String currentDate = sdf.format(new Date());
    return currentDate;
  }

  
  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();
  }

  
  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();
  }

  
  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();
  }
  
  public static String getYear(String DateString){

    return DateString.substring(0,4);
  }

  
  public static String getMonth(String DateString){

    return DateString.substring(4,6);
  }

  
  public static String getDayNoTime(String DateString){
    return DateString.substring(6);
  }

  
  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;
  }

  
  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;
  }

  
  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;
  }

  
  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;
  }

  
  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));
  }

  
  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);
  }

  
  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;
  }

  
  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;
  }

  
  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;
  }

  
  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;
  }

  
  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;
  }

  
  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;
  }

  
  public static int getCurrentYear() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(1);
  }

  
  public static int getCurrentMonth() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(2) + 1;
  }

  
  public static int getCurrentDay() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(5);
  }

  
  public static int getCurrentHour() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(11);
  }

  
  public static int getCurrentMinute() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(12);
  }

  
  public static int getCurrentSecond() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(13);
  }

  
  public static int getYear(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(1);
  }

  
  public static int getMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(2) + 1;
  }

  
  public static int getDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(5);
  }

  
  public static int getHour(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(11);
  }

  
  public static int getMinute(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(12);
  }

  
  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 map = new HashMap();
    map.put("date", new Date());
    String json = JSONObject.fromObject(map).toString();
    System.out.println(json);
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号