zoukankan      html  css  js  c++  java
  • java日期函数工具类

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
     
    public class DateTimeUtil {
    public static String getDateStr(String pattern,Date date){
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    return df.format(date);
    }
    public static Date getDateFromStr(String pattern,String dateStr)throws Exception{
    if(dateStr == null){
    return null;
    }
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    return df.parse(dateStr);
    }
    public static Date addYear(Date date,int amount){
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.YEAR, amount);
    return cal.getTime();
    }
    public static Date addMonth(Date date,int amount){
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.MONTH, amount);
    return cal.getTime();
    }
    public static Date addDate(Date date,int amount){
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.DATE, amount);
    return cal.getTime();
    }
    public static Date addHour(Date date,int amount){
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.HOUR, amount);
    return cal.getTime();
    }
    public static Date addMinute(Date date,int amount){
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.MINUTE, amount);
    return cal.getTime();
    }
    }
  • 相关阅读:
    Spring学习笔记
    Bash编程(6) String操作
    Bash编程(5) Shell方法
    Bash编程(4) 参数与变量
    Bash编程(2) 循环与分支
    CentOS 升级 openSSH
    Bash编程(1) 基础
    DNS配置
    资料收集:学习 Linux/*BSD/Unix 的 30 个最佳在线文档
    【数位DP】[LOJ10168] 恨7不成妻
  • 原文地址:https://www.cnblogs.com/wangdonghua/p/3320852.html
Copyright © 2011-2022 走看看