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

java时间戳与日期相互转换工具详解

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

java时间戳与日期相互转换工具详解

本文为大家分享了java日期与时间戳相互转换大全,供大家参考,具体内容如下

package com.crm.util; 
 
import java.math.BigDecimal; 
import java.text.DecimalFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
 
 
 
public class DateFormatUtil { 
   
   
  public static Date transForDate(Integer ms){ 
    if(ms==null){ 
      ms=0; 
    } 
    long msl=(long)ms*1000; 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date temp=null; 
    if(ms!=null){ 
      try { 
 String str=sdf.format(msl); 
 temp=sdf.parse(str); 
      } catch (ParseException e) { 
 e.printStackTrace(); 
      } 
    } 
    return temp; 
  } 
   
   
  public static int getTimes(int day, int hour, int minute) { 
    Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.DATE, day); 
    cal.set(Calendar.HOUR_OF_DAY, hour); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MINUTE, minute); 
    cal.set(Calendar.MILLISECOND, 0); 
    return (int) (cal.getTimeInMillis() / 1000); 
  } 
   
   
  public static int getIntegralTime() { 
    Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.HOUR_OF_DAY, 1); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.MILLISECOND, 0); 
    return (int) (cal.getTimeInMillis() / 1000); 
  } 
   
  public static int getIntegralTimeEnd() { 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, 24); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.MILLISECOND, 0); 
    return (int) (cal.getTimeInMillis() / 1000); 
  } 
   
   
  public static Date transForDate3(Integer ms){ 
    if(ms==null){ 
      ms=0; 
    } 
    long msl=(long)ms*1000; 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
    Date temp=null; 
    if(ms!=null){ 
      try { 
 String str=sdf.format(msl); 
 temp=sdf.parse(str); 
      } catch (ParseException e) { 
 e.printStackTrace(); 
      } 
    } 
    return temp; 
  } 
   
  public static Date transForDate(Long ms){ 
    if(ms==null){ 
      ms=(long)0; 
    } 
    long msl=(long)ms*1000; 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date temp=null; 
    if(ms!=null){ 
      try { 
 String str=sdf.format(msl); 
 temp=sdf.parse(str); 
      } catch (ParseException e) { 
 e.printStackTrace(); 
      } 
    } 
    return temp; 
  } 
   
   
  public static String transForDate1(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

      if(ms!=null){ 
 try { 
   str=sdf.format(msl); 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } 
      } 
    }     
    return str; 
  } 
  public static String transForDate2(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 

      if(ms!=null){ 
 try { 
   str=sdf.format(msl); 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } 
      } 
    }     
    return str; 
  } 
   
  public static String transForDate4(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd"); 

      if(ms!=null){ 
 try { 
   str=sdf.format(msl); 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } 
      } 
    }     
    return str; 
  } 
   
   
  public static String transForDate5(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 

      if(ms!=null){ 
 try { 
   str=sdf.format(msl); 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } 
      } 
    }     
    return str; 
  } 
   
  public static String transForDateInChinese(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 

      if(ms!=null){ 
 try { 
   str=sdf.format(msl); 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } 
      } 
    }     
    return str; 
  } 
   
   
  public static Integer transForMilliSecond(Date date){ 
    if(date==null) return null; 
    return (int)(date.getTime()/1000); 
  } 
   
   
  public static Integer currentTimeStamp(){ 
    return (int)(System.currentTimeMillis()/1000); 
  } 
   
   
  public static Integer transForMilliSecond(String dateStr){ 
    Date date = DateFormatUtil.formatDate(dateStr); 
    return date == null ? null : DateFormatUtil.transForMilliSecond(date); 
  } 
   
  public static Integer transForMilliSecond(String dateStr,String format){ 
    Date date = DateFormatUtil.formatDate(dateStr,format); 
    return date == null ? null : DateFormatUtil.transForMilliSecond(date); 
  } 
   
  public static Integer transForMilliSecondByTim(String dateStr,String tim){ 
    SimpleDateFormat sdf=new SimpleDateFormat(tim); 
    Date date =null; 
    try { 
      date = sdf.parse(dateStr); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return date == null ? null : DateFormatUtil.transForMilliSecond(date); 
  } 
   
  public static Date formatDate(String dateStr){ 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date result=null; 
    try { 
      result = sdf.parse(dateStr); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return result; 
  } 
   
  public static Date formatDate(String dateStr,String format){ 
    SimpleDateFormat sdf=new SimpleDateFormat(format); 
    Date result=null; 
    try { 
      result = sdf.parse(dateStr); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return result; 
  } 
   
  public static String formatDate(Date date){ 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String result=null; 
    result = sdf.format(date); 
    return result; 
  } 
   
  public static String formatDate(Date date,String format){ 
    SimpleDateFormat sdf=new SimpleDateFormat(format); 
    String result=null; 
    result = sdf.format(date); 
    return result; 
  } 
   
  public static String transForDate(Integer ms, String format){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat(format); 
      if(!ms.equals(0)){ 
 try { 
   str=sdf.format(msl); 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } 
      } 
    } 
    return str; 
  }   
   
   
  public static String splitBigDecimal(BigDecimal b, int mode) { 
    DecimalFormat df = new DecimalFormat("0.00"); 
    String s = df.format(b); 
    if(mode==0){ 
      return s.split("\.")[0]; 
    }else { 
      return "."+s.split("\.")[1]; 
    } 
  } 
   
   
  public static int caculate2Days(Integer ts1, Integer ts2) { 
    Date firstDate = DateFormatUtil.transForDate(ts1); 
    Date secondDate = DateFormatUtil.transForDate(ts2); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(firstDate); 
    int dayNum1 = calendar.get(Calendar.DAY_OF_YEAR); 
    calendar.setTime(secondDate); 
    int dayNum2 = calendar.get(Calendar.DAY_OF_YEAR); 
    return Math.abs(dayNum1 - dayNum2); 
  } 
   
   
  public String mobileSerect(String mobile){ 
    if(!StringUtils.isBlank(mobile)){ 
      int between = mobile.length()/2; 
      mobile = mobile.substring(0, between-2)+"****"+mobile.substring(between+2, mobile.length()); 
    } 
    return mobile; 
  } 
   
   
  public String emailSerect(String email) { 
    if(!StringUtils.isBlank(email)){ 
      int length = email.lastIndexOf("@"); 
      email = email.substring(0, 2)+"****"+email.substring(length-2, email.length()); 
    } 
    return email; 
  } 
   
   
  public BigDecimal sumBigDicimal(BigDecimal source, BigDecimal target) { 
    source = source.add(target); 
    return source; 
  } 
   
   
  public BigDecimal sumBigDicimalAndDouble(BigDecimal source, Double target) { 
    BigDecimal new_target = new BigDecimal(target); 
    source = source.add(new_target); 
    return source; 
  } 
   
   
  public BigDecimal subBigDicimal(BigDecimal source, BigDecimal target) { 
    source = source.subtract(target); 
    return source; 
  } 
   
   
  public static Long getTimediff(int timeStamp){ 
    Date d1 = DateFormatUtil.transForDate(timeStamp); 
    Date today = new Date(); 
    if(d1.getTime()=today.getTime()){ 
      return true; 
    } 
    return false; 
  } 
 
   
   
   
  public static int transFromTime(String time){ 
    return transForMilliSecond("1970-01-01 "+time,"yyyy-mm-dd HH:mm:ss"); 
  } 
   
   
  public static String transToTime(int time){ 
    String s = new String(transForDate1(time)); 
    String ss[] = s.split(" "); 
    return ss[1]; 
  } 
   
  public static int transToChuo(String dateString){ 
    SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd"); 
    int res = 0; 
    try { 
      Date date=simpleDateFormat .parse(dateString); 
      res = (int) date.getTime(); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return res; 
  } 
   
  public static void main(String[] args) { 
     
    //System.out.println(getIntegralTimeEnd()); 
    System.out.println(transForDate2(transForMilliSecond("2015-02-25 00:00:00"))); 
    //System.out.println(transForMilliSecond("2016-01-25","yyyy-mm-dd")); 
    //System.out.println(transForDate1(transForMilliSecond("1970-01-01 00:00:00","yyyy-mm-dd HH:mm:ss"))); 
    //System.out.println(currentTimeStamp()); 
    //System.out.println(transForDate(currentTimeStamp())); 
    //System.out.println(new Date()); 
    //System.out.println(DateUtils.getDate()); 
    System.out.println(transFromTime("00:00:01")); 
    System.out.println(transToTime(transFromTime("15:01:13"))); 
  } 
} 

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

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

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

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