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

日期处理工具类

作者头像
IT云清
发布2019-01-22 11:10:53
5080
发布2019-01-22 11:10:53
举报
文章被收录于专栏:IT云清
代码语言:javascript
复制
package utils;

import org.apache.commons.lang.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by lightClouds917
 * Date 2018/1/9
 * Description:date util
 */
public class MyDateUtil {

    /**test those methods*/
    public static void main(String[] args) throws Exception{
        Date date = int2Date(42237);
        //Fri Aug 21 00:00:00 CST 2015
        System.out.println(int2Date(42237));
        //2015.8.21
        System.out.println(getYearOrMonthOrDay(date,"year"));
        //2015
        System.out.println(getYearOrMonthOrDay(date,"month"));
        //8
        System.out.println(getYearOrMonthOrDay(date,"day"));
        //21
        Date dateByString1 = string2Date("2017.7.1");
        System.out.println(dateByString1.toString());
        //Sat Jul 01 00:00:00 CST 2017
        System.out.println(date2String1(dateByString1));
        //2017年7月1日
        System.out.println(date2String2(dateByString1));
        //2017-7-1
        System.out.println(string2Date("2017-8-15"));
        //Tue Aug 15 00:00:00 CST 2017
        System.out.println(string2Date("2017年8月15日"));
        //Tue Aug 15 00:00:00 CST 2017
        System.out.println(getNowYear());
        //2018
        System.out.println(getNowMonth());
        //3

    }

    /**
     * int类型转为日期类
     * 一般用于解析Excel
     *
     * parse date by int
     * we use it always in parsing Excel
     * @param dateNum
     * @return
     */
    public static Date int2Date(Integer dateNum){
        Calendar calendar = new GregorianCalendar(1900,0,-1);
        Date dateRe = DateUtils.addDays(calendar.getTime(),dateNum);
        return dateRe;
    }

    /**
     * get year or month or day from date which instance of Date
     * @param date
     * @param type
     * @return
     */
    public static Integer getYearOrMonthOrDay(Date date,String type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if("year".equals(type)){
            return calendar.get(Calendar.YEAR);
        }else if("month".equals(type)){
            return calendar.get(Calendar.MONTH)+1;
        }else if("day".equals(type)){
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
        return null;
    }

    /**
     * parse String to Date
     * @param str
     * @return
     * @throws Exception
     */
    public static Date string2Date(String str) throws Exception{
        Date date = null;
        if(str.contains("/")){
            str = str.replace("/","-");
        }else if(str.contains(".")){
            str = str.replace(".","-");
        }else if(!str.contains("日") && str.contains("年") && str.contains("月")){
            str = str.replace("年","-");
            str = str.replace("月","");
        }else if(str.contains("日") && str.contains("年") && str.contains("月")){
            str = str.replace("年","-");
            str = str.replace("月","-");
            str = str.replace("日","");
        }
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            date = format1.parse(str);
        }catch (Exception ex){
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM");
            date = format2.parse(str);
        }
        return date;
    }

    /**
     * parse Date to String   eg:2017年7月1日
     * @param date
     * @return
     */
    public static String date2String1(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String str = calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日";
        return str;
    }

    /**
     * parse Date to String   eg:2017-7-1
     * @param date
     * @return
     */
    public static String date2String2(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String str = calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH);
        return str;
    }

    /**
     * get now year eg:2018
     */
    public static Integer getNowYear(){
        Calendar cal = Calendar.getInstance();
        int nowYear = cal.get(Calendar.YEAR);
        return nowYear;
    }

    /**
     * get now month  eg:8
     */
    public static Integer getNowMonth(){
        Calendar cal = Calendar.getInstance();
        int nowMonth = cal.get(Calendar.MONTH) + 1;
        return nowMonth;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年03月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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