Java基础系列之日期时间处理

来源:互联网 发布:淘宝1001夜视频 编辑:程序博客网 时间:2024/05/17 23:37

 Java在处理时间方面提供了很多的API供我们开发使用。例如:Date,Calendar,SimpleDateFormat等等之类的。下面看例子Demo的实现过程:

package myapi.time.demo;import java.math.BigDecimal;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class JavaTimeApi {/**      * 返回当前日期时间字符串<br>      * 默认格式:yyyy-mm-dd hh:mm:ss      *       * @return String 返回当前字符串型日期时间      */      public static String getCurrentTime() {           SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          Date date = new Date();          return f.format(date);      }              /**      * 返回当前日期时间字符串<br>      * 默认格式:yyyymmddhhmmss      *       * @return String 返回当前字符串型日期时间      */      public static BigDecimal getCurrentTimeNumber() {          String returnStr = null;          SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");          Date date = new Date();          returnStr = f.format(date);          return new BigDecimal(returnStr);      }            /**      * 返回自定义格式的当前日期时间字符串      *       * @param format      *            格式规则      * @return String 返回当前字符串型日期时间      */      public static String getCurrentTime(String format) {          String returnStr = null;          SimpleDateFormat f = new SimpleDateFormat(format);          Date date = new Date();          returnStr = f.format(date);          return returnStr;      }          /**      * 返回指定格式的字符型日期      * @param date      * @param formatString      * @return      */      public static String getCurrentTime(Date date, String formatString) {          if (date==null) {              return null;          }          SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);          String strDate = simpledateformat.format(date);          return strDate;      }              /**      * 返回当前字符串型日期      *       * @param format      *            格式规则      *       * @return String 返回的字符串型日期      */      public static String getCurDate(String format) {          Calendar calendar = Calendar.getInstance();          SimpleDateFormat simpledateformat = new SimpleDateFormat(format);          String strDate = simpledateformat.format(calendar.getTime());          return strDate;      }          /**      * 将字符串型日期转换为日期型      *       * @param strDate      *            字符串型日期      * @param srcDateFormat      *            源日期格式      * @param dstDateFormat      *            目标日期格式      * @return Date 返回的util.Date型日期      */      public static Date getCurrentTime(String strDate, String srcDateFormat, String dstDateFormat) {          Date rtDate = null;          Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));          String tmpString = null;          if (tmpDate != null)              tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);          if (tmpString != null)              rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));          return rtDate;      }          /**      * 将字符串型日期转换为日期型      *       * @param strDate      *            字符串型日期      * @param srcDateFormat      *            输出日期格式      * @return Date 返回的util.Date型日期      */      public static Date getDate(String strDate, String dateFormat) {          Date tmpDate = (new SimpleDateFormat(dateFormat)).parse(strDate, new ParsePosition(0));          return tmpDate;      }             /**      * 将字符串型日期转换为日期型      *       * @param strDate      *            字符串型日期      * @return Date 返回的util.Date型日期      */      public static Date getDate(String strDate) {          Date tmpDate = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(strDate, new ParsePosition(0));          return tmpDate;      }     public static int getLastDayOfMonth(int year,int month){        Calendar cal = Calendar.getInstance();        //设置年份        cal.set(Calendar.YEAR,year);        //设置月份        cal.set(Calendar.MONTH, month-1);        //获取某月最大天数        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);        //设置日历中月份的最大天数        cal.set(Calendar.DAY_OF_MONTH, lastDay);        //格式化日期        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        String lastDayOfMonth = sdf.format(cal.getTime());        return Integer.valueOf(lastDayOfMonth.substring(lastDayOfMonth.length()-2, lastDayOfMonth.length()));    }        public static String getLastDayOfMonth(String strDate){    Date date=getDate(strDate);        Calendar cal = Calendar.getInstance();    cal.setTime(date);        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);        cal.set(Calendar.DAY_OF_MONTH, lastDay);        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        String lastDayOfMonth = sdf.format(cal.getTime());        return lastDayOfMonth;    }public static void main(String[] args) {//String currentTime=JavaTimeApi.getCurrentTime();//System.out.println("current time is "+currentTime);//BigDecimal currentTime=JavaTimeApi.getCurrentTimeAsNumber();//System.out.println("current time is "+currentTime);//String currentTime=JavaTimeApi.getCurrentTime("yyyy-MM-dd HH:mm:ss");//System.out.println(currentTime);//String currentTime=JavaTimeApi.Date2String(new Date(),"yyyy-MM-dd HH:mm:ss");//System.out.println(currentTime);//String currentTime=JavaTimeApi.getCurDate("yyyy-MM-dd HH:mm:ss");//System.out.println(currentTime);//Date currentTime=JavaTimeApi.stringToDate("2016-07-12 20:39:20","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd");//String str=JavaTimeApi.Date2String(currentTime,"yyyy-MM-dd");//System.out.println(str);//String str=JavaTimeApi.getLastDayOfMonth(2016,7);//System.out.println(str);//String str=JavaTimeApi.getLastDayOfMonth("2016-7-13 12:12:12");//System.out.println("str="+str);}}
上面的Demo基本设计到了平常日期处理的大部分问题,例如String类型的日期转换成Date类型,把Date转换为指定格式的String类型等等之类的。

0 0