大量java日期格式化、日期处理函数

        最近在程序开发中整理了很多日期格式化的函数,发布上来希望对大家有用

  1. /*********************************************************************
  2.  * <p>Title: GetDate</p>
  3.  * <p>Description: 常用日期格式化、日期处理函数</p>
  4.  * <p>Copyright: Copyright (c) 2008</p>
  5.  * <p>Company: ...</p>
  6.  * @author rain
  7.  * @version 1.0
  8.  *********************************************************************/
  9. import java.text.SimpleDateFormat;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. import java.util.GregorianCalendar;
  13. import com.neusoft.qd.sapinterface.sysconfig.SysConfig;
  14. public class GetDate {
  15.     public GetDate() {
  16.     }
  17.     /**
  18.      * 获取当前时间(日期+时间)
  19.      * 返回时间格式是:yyyy-MM-dd HH:mm:ss
  20.      * @return String
  21.      */
  22.     public static String getCurrentDate() {
  23.         String dateString = "";
  24.         Calendar calendar = null;
  25.         SimpleDateFormat formatter = null;
  26.         try {
  27.             calendar = new GregorianCalendar();
  28.             formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  29.             dateString = formatter.format(calendar.getTime());
  30.         } catch (Exception ex) {
  31.             ex.printStackTrace();
  32.         }
  33.         return dateString;
  34.     }
  35.     /**
  36.      * 获取当前时间(时分秒)
  37.      * 返回时间格式可以为:
  38.      * HH点mm分ss秒(11点10分15秒);
  39.      * HHmmss(111015);
  40.      * HH:mm:ss(11:10:15)
  41.      * @return String
  42.      */
  43.     public static String getCurrentTime() {
  44.         String dateString = "";
  45.         Calendar calendar = null;
  46.         SimpleDateFormat formatter = null;
  47.         try {
  48.             calendar = new GregorianCalendar();
  49.             //打印时间格式为HHmmss,例如11点10分20秒的格式:111020
  50.             //formatter = new SimpleDateFormat("HHmmss");
  51.             //打印时间格式为HH:mm:ss,例如11点10分20秒的格式:11点10分20秒
  52.             //formatter = new SimpleDateFormat("HH点mm分ss秒");
  53.             //打印时间格式为HH:mm:ss,例如11点10分20秒的格式:11:10:20
  54.             formatter = new SimpleDateFormat("HH:mm:ss");
  55.             dateString = formatter.format(calendar.getTime());
  56.         } catch (Exception ex) {
  57.             ex.printStackTrace();
  58.         }
  59.         return dateString;
  60.     }
  61.     /**
  62.      * 获取时间(yyyy-mm-dd格式)
  63.      * 例如,2008-08-20
  64.      * @return String
  65.      */
  66.     public static String getCurrentDay() {
  67.         String dateString = "";
  68.         Calendar calendar = null;
  69.         SimpleDateFormat formatter = null;
  70.         try {
  71.             calendar = new GregorianCalendar();
  72.             formatter = new SimpleDateFormat("yyyy-MM-dd");
  73.             dateString = formatter.format(calendar.getTime());
  74.         } catch (Exception ex) {
  75.             ex.printStackTrace();
  76.         }
  77.         return dateString;
  78.     }
  79.     /**
  80.      * 获取时间,格式是(yyyy-mm-dd)
  81.      * 获取当前时间,减去从sysconfig.getDatebegin()获取的天数
  82.      * 例如今天是2008-08-20,sysconfig.getDatebegin()的值是7,
  83.      * 那么返回值为2008-08-13,以此类推
  84.      * @return String
  85.      */
  86.     public static String getCurrentDaySubtract() {
  87.         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  88.         Calendar now = Calendar.getInstance();
  89.         try {
  90.             SysConfig sysconfig = new SysConfig();//读取指定文件中字段信息
  91.             now.add(Calendar.DAY_OF_WEEK, Integer.parseInt(sysconfig.getDatebegin()));
  92.         } catch (Exception e) {
  93.             e.printStackTrace();
  94.         }
  95.         return df.format(now.getTime());
  96.     }
  97.     /**
  98.      * 获取当前时间,时间格式(yyyymmdd)
  99.      * 例如,20080820
  100.      * @return String
  101.      */
  102.     public static String getCurrentDayYmd() {
  103.         String dateString = "";
  104.         Calendar calendar = null;
  105.         SimpleDateFormat formatter = null;
  106.         try {
  107.             calendar = new GregorianCalendar();
  108.             formatter = new SimpleDateFormat("yyyyMMdd");
  109.             dateString = formatter.format(calendar.getTime());
  110.         } catch (Exception ex) {
  111.         }
  112.         return dateString;
  113.     }
  114.     /**
  115.      * 获取时间(yyyymmdd格式)
  116.      * 获取两天前的时间
  117.      * @return String
  118.      */
  119.     public static String getLast2Ymd() {
  120.         String dateString = "";
  121.         Calendar calendar = null;
  122.         SimpleDateFormat formatter = null;
  123.         try {
  124.             calendar = new GregorianCalendar();
  125.             formatter = new SimpleDateFormat("yyyyMMdd");
  126.             calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar
  127.                     .get(java.util.Calendar.DAY_OF_MONTH) - 2);
  128.             dateString = formatter.format(calendar.getTime());
  129.         } catch (Exception ex) {
  130.         }
  131.         return dateString;
  132.     }
  133.     /**
  134.      * 获取时间(yyyy-mm-dd格式)
  135.      * 获取当前日期加一后的日期,即明天的日期
  136.      * @return String
  137.      */
  138.     public static String getTom() {
  139.         String dateString = "";
  140.         try {
  141.             java.util.Date pre_date = new java.util.Date(System.currentTimeMillis()
  142.                     + 1 * 24 * 3600 * 1000);
  143.             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  144.             dateString = formatter.format(pre_date);
  145.         } catch (Exception ex) {
  146.         }
  147.         return dateString;
  148.     }
  149.     
  150.     /**
  151.      * 获取指定周周一对应的日期,日期格式(yyyy-MM-dd)
  152.      * 输入参数int iNext对应的值是几,那么就计算几个周之前或之后的周一对应的日期
  153.      * 参数int iNext可为正值或负值
  154.      * 例如int iNext,值为1时,计算的是下个周周一对应的日期时间,
  155.      * int iNext,值为-1时,计算的是上个周周一对应的日期时间,
  156.      * @return String
  157.      */
  158.     public static String getNextMonday(int iNext, String dateFormat) {
  159.         String strRet = "";
  160.         Date dt = new Date();
  161.         GregorianCalendar gc = new GregorianCalendar();
  162.         gc.setTime(dt);
  163.         gc.set(72);
  164.         gc.add(57 * iNext);
  165.         if (dateFormat != null && !dateFormat.equals("")) {
  166.             SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
  167.             strRet = formatter.format(gc.getTime());
  168.         } else {
  169.             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  170.             strRet = formatter.format(gc.getTime());
  171.         }
  172.         return strRet;
  173.     }
  174.     
  175.     /**
  176.      * 获取指定周周日对应的日期,日期格式(yyyy-MM-dd)
  177.      * 输入参数int iNext对应的值是几,那么就计算几个周之前或之后的周日对应的日期
  178.      * 参数int iNext可为正值或负值
  179.      * 例如int iNext,值为1时,计算的是下个周周日对应的日期时间,
  180.      * int iNext,值为-1时,计算的是上个周周日对应的日期时间,
  181.      * @return String
  182.      */
  183.   public static String getNextSunday(int iNext, String dateFormat) {
  184.     String strRet = "";
  185.     Date dt = new Date();
  186.     GregorianCalendar gc = new GregorianCalendar();
  187.     gc.setTime(dt);
  188.     gc.set(71);
  189.     gc.add(57 * iNext);
  190.     if (dateFormat != null && !dateFormat.equals("")) {
  191.       SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
  192.       strRet = formatter.format(gc.getTime());
  193.     }
  194.     else {
  195.       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  196.       strRet = formatter.format(gc.getTime());
  197.     }
  198.     return strRet;
  199.   }
  200.     
  201.     public static void main(String[] agrv) {
  202.         System.out.println("函数getCurrentDate()打印的日期格式       :"
  203.                 + GetDate.getCurrentDate());
  204.         System.out.println("函数getCurrentTime()打印的日期格式       :"
  205.                 + GetDate.getCurrentTime());
  206.         System.out.println("函数getCurrentDay()打印的日期格式        :"
  207.                 + GetDate.getCurrentDay());
  208.         System.out.println("函数getCurrentDaySubtract打印的日期格式  :"
  209.                 + GetDate.getCurrentDaySubtract());
  210.         System.out.println("函数getCurrentDayYmd()打印的日期格式     :"
  211.                 + GetDate.getCurrentDayYmd());
  212.         System.out.println("函数getLast2Ymd()打印的日期格式          :"
  213.                 + GetDate.getLast2Ymd());
  214.         System.out.println("函数getTom()打印的日期格式               :" + GetDate.getTom());
  215.         System.out.println("函数getNextMonday()打印的日期格式        :"
  216.                 + GetDate.getNextMonday(1""));
  217.         System.out.println("函数getNextSunday()打印的日期格式        :"
  218.                 + GetDate.getNextSunday(1""));
  219.     }
  220. }

2008年08月21日运行main函数后打印结果如下:

函数getCurrentDate()打印的日期格式            :2008-08-21 11:32:59
函数getCurrentTime()打印的日期格式            :11:32:59
函数getCurrentDay()打印的日期格式             :2008-08-21
函数getCurrentDaySubtract打印的日期格式   :2008-08-14
函数getCurrentDayYmd()打印的日期格式      :20080821
函数getLast2Ymd()打印的日期格式               :20080819
函数getTom()打印的日期格式                        :2008-08-22
函数getNextMonday()打印的日期格式           :2008-08-25
函数getNextSunday()打印的日期格式             :2008-08-24

 

过几天将发布整理后的常用工具类,例如:编码格式转换、分割、替换子串、追加等功能。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值