首页 > 其他分享 >日期处理工具类

日期处理工具类

时间:2023-01-14 21:32:39浏览次数:35  
标签:calendar return 处理 Date 日期 str date Calendar 工具


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;
}
}


标签:calendar,return,处理,Date,日期,str,date,Calendar,工具
From: https://blog.51cto.com/u_15936016/6007816

相关文章

  • Kubernetes:通过轻量化工具 kubespy 实时观察YAML资源变更
    写在前面分享一个小工具​​kubespy​​给小伙伴博文内容涉及:工具的简单介绍下载安装以​​kubectl​​插件方式使用Demo理解不足小伙伴帮忙指正我所渴求的,無非是......
  • [VueJsDev] 基础知识 - asyncTool.js异步执行工具
    asyncTool.js异步执行工具:::details目录目录​asyncTool.js异步执行工具​​​Step.1:getAc使用方法​​​​Meth.2:use方法​​​​Meth.3:run方法​​​​M......
  • lightdb中日期加减
    在PostgreSQL中可以直接对时间进行加减运算:、SELECTnow()::timestamp+'1year'; --当前时间加1年SELECTnow()::timestamp+'1month'; --当前时间加一个月SELE......
  • linux工具grep的使用心得笔记
    grep作为linux管理中常用的三大工具之一(grep、awk、sed),其功能十分强大,因此难以对其进行全面的使用介绍,因此本文只作为个人学习的笔记之用。 grep的用处:在文本中匹配要......
  • 字符串处理
    《面对字符串输入的情况》  想这个输入格式我该如何输入?这样即可:scanf("%d:(%d)",&s,&cnt); 《字符串哈希》对于字符串str长度为n,可以在O(n)的时间内解......
  • java:时间显示------(日期格式化:format)
    format方法:格式化模式format方法中的“格式化模式”是一个用双引号括起的字符序列,该字符序列中的字符由时间格式符和普通字符所构成。例如假设当前时间是2016/10......
  • 经纬度工具类GPSUtil
    packagecom.new3s.manage.util;importorg.apache.commons.lang3.StringUtils;publicclassGPSUtil{publicstaticStringgpsToDecimals(StringgpsParam)......
  • 20 个 JS 工具函数助力高效开发
    日常开发中,面对各种不同的需求,我们经常会用到以前开发过的一些工具函数,把这些工具函数收集起来,将大大提高我们的开发效率。1、校验数据类型export const typeOf = fu......
  • Linux下图片处理
    Linux下图片处理   图片是指由图形、图像等构成的平面媒体。图片的格式很多,但总体上可以分为点阵图和矢量图两大类,我们常用BMP、JPG等格式都是点阵图形,而SWF、CDR、AI......
  • img标签图片加载异常处理|图片加载默认显示
    img标签图片加载异常处理img标签中的src如果加载失败会显示错误文件,可以利用以下方法处理异常图片VUE写法页面<img:src="scope.row.picUrl"alt=""@load="imgLoad"......