前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java基础系列之日期时间处理

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

作者头像
林老师带你学编程
发布2018-01-03 17:11:03
8770
发布2018-01-03 17:11:03
举报
文章被收录于专栏:强仔仔

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

代码语言:javascript
复制
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类型等等之类的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年07月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档