一个处理Date与String的工具类
public class DateUtil {
private DateUtil(){
}
public static final String hhmmFormat="HH:mm";
public static final String MMddFormat="MM-dd";
public static final String yyyyFormat="yyyy";
public static final String yyyyChineseFormat="yyyy年";
public static final String yyyyMMddFormat="yyyy-MM-dd";
public static final String fullFormat="yyyy-MM-dd HH:mm:ss";
public static final String MMddChineseFormat="MM月dd日";
public static final String yyyyMMddChineseFormat="yyyy年MM月dd日";
public static final String fullChineseFormat="yyyy年MM月dd日 HH时mm分ss秒";
public static final String [] WEEKS={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
public static final String [] WEEKS_EN={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
public static final String YMD_FORMAT = "yyyyMMdd";
/**
* 得到指定时间的时间日期格式
* @param date 指定的时间
* @param format 时间日期格式
* @return
*/
public static String getFormatDateTime(Date date,String format){
DateFormat df=new SimpleDateFormat(format);
return df.format(date);
}
/**
* 得到指定时间的时间日期格式
* @param date 指定的时间
* @param format 时间日期格式
* @param land 语言类型 en=英文,zh_CN=中文
* @return
*/
public static String getFormatDateTime(Date date,String format,String land){
String time;
String week;
DateFormat df=new SimpleDateFormat(format);
time = df.format(date);
week = land.equals("zh_CN") ? WEEKS[getWeek(date)-1] : WEEKS_EN[getWeek(date)-1];
time = time +" "+week;
return time;
}
public static String formatDate(Date date, String format){
DateFormat df=new SimpleDateFormat(format);
return df.format(date);
}
public static String formatDateYmd(Date date){
DateFormat df=new SimpleDateFormat("yyyyMMdd");
return df.format(date);
}
public static String formatDateYmdHms(Date date){
DateFormat df=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
return df.format(date);
}
public static Date stringToDateYmd(String dateString) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = null;
try {
date = simpleDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String formattedTime = simpleDateFormat.format(date);
date = java.sql.Date.valueOf(formattedTime);
return date;
}
public static Date stringToDateYmdHms(String dateString) {
Date date = null;
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = sdf.parse(dateString);
}
catch (ParseException e)
{
System.out.println(e.getMessage());
}
return date;
}
public static Date parseDateYmdHms(String dateString) {
Date date = null;
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
date = sdf.parse(dateString);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
return date;
}
/**
* 判断是否是润年
* @param date 指定的时间
* @return true:是润年,false:不是润年
*/
public static boolean isLeapYear(Date date) {
Calendar cal=Calendar.getInstance();
cal.setTime(date);
return isLeapYear(cal.get(Calendar.YEAR));
}
/**
* 判断是否是润年
* @param year 指定的年
* @return true:是润年,false:不是润年
*/
public static boolean isLeapYear(int year) {
GregorianCalendar calendar = new GregorianCalendar();
return calendar.isLeapYear(year);
}
/**
* 判断指定的时间是否是今天
* @param date 指定的时间
* @return true:是今天,false:非今天
*/
public static boolean isInToday(Date date){
boolean flag=false;
Date now=new Date();
String fullFormat=getFormatDateTime(now, DateUtil.yyyyMMddFormat);
String beginString=fullFormat+" 00:00:00";
String endString=fullFormat+" 23:59:59";
DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
try {
Date beginTime=df.parse(beginString);
Date endTime=df.parse(endString);
flag=date.before(endTime)&&date.after(beginTime);
} catch (ParseException e) {
e.printStackTrace();
}
return flag;
}
/**
* 判断两时间是否是同一天
* @param from 第一个时间点
* @param to 第二个时间点
* @return true:是同一天,false:非同一天
*/
public static boolean isSameDay(Date from,Date to){
boolean isSameDay=false;
DateFormat df=new SimpleDateFormat(DateUtil.yyyyMMddFormat);
String firstDate=df.format(from);
String secondDate=df.format(to);
isSameDay=firstDate.equals(secondDate);
return isSameDay;
}
/**
* 求出指定的时间那天是星期几
* @param date 指定的时间
* @return 星期X
*/
public static String getWeekString(Date date){
return DateUtil.WEEKS[getWeek(date)-1];
}
/**
* 求出指定时间那天是星期几
* @param date 指定的时间
* @return 1-7
*/
public static int getWeek(Date date){
int week=0;
Calendar cal=Calendar.getInstance();
cal.setTime(date);
week=cal.get(Calendar.DAY_OF_WEEK);
return week;
}
/**
* 取得指定时间离现在是多少时间以前,如:3秒前,2小时前等
* 注意:此计算方法不是精确的
* @param date 已有的指定时间
* @return 时间段描述
*/
public static String getAgoTimeString(Date date){
Date now=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
Date agoTime=cal.getTime();
long mtime=now.getTime()-agoTime.getTime();
String str="";
long stime=mtime/1000;
long minute=60;
long hour=60*60;
long day=24*60*60;
long weeks=7*24*60*60;
long months=100*24*60*60;
if(stime<minute){
long time_value=stime;
if(time_value<=0){
time_value=1;
}
str=time_value+"秒前";
}else if(stime>=minute && stime<hour){
long time_value=stime/minute;
if(time_value<=0){
time_value=1;
}
str=time_value+"分前";
}else if(stime>=hour && stime<day){
long time_value=stime/hour;
if(time_value<=0){
time_value=1;
}
str=time_value+"小时前";
}else if(stime>=day&&stime<weeks){
long time_value=stime/day;
if(time_value<=0){
time_value=1;
}
str=time_value+"天前";
}else if(stime>=weeks&&stime<months){
DateFormat df=new SimpleDateFormat(DateUtil.MMddFormat);
str=df.format(date);
}else{
DateFormat df=new SimpleDateFormat(DateUtil.yyyyMMddFormat);
str=df.format(date);
}
return str;
}
/**
* 判断指定时间是否是周末
* @param date 指定的时间
* @return true:是周末,false:非周末
*/
public static boolean isWeeks(Date date){
boolean isWeek=false;
isWeek=(getWeek(date)-1==0||getWeek(date)-1==6);
return isWeek;
}
/**
* 获取当前日期
* @return Date
* */
public static Date getToday(){
String date = DateUtil.formatDate(new Date(), YMD_FORMAT);
return DateUtil.stringToDateYmd(date);
}
/**
* 获取当前日期
* @return Date
* */
public static String getTodayStrYmd(){
String date = DateUtil.formatDate(new Date(), YMD_FORMAT);
return date;
}
/**
* 得到今天的最开始时间
* @return 今天的最开始时间
*/
public static Date getTodayBeginTime(){
String beginString= DateUtil.yyyyMMddFormat+" 00:00:00";
DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
Date beginTime=new Date();
try {
beginTime=df.parse(beginString);
} catch (ParseException e) {
e.printStackTrace();
}
return beginTime;
}
/**
* 得到今天的最后结束时间
* @return 今天的最后时间
*/
public static Date getTodayEndTime(){
String endString= DateUtil.yyyyMMddFormat+" 23:59:59";
DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
Date endTime=new Date();
try {
endTime=df.parse(endString);
} catch (ParseException e) {
e.printStackTrace();
}
return endTime;
}
/**
* 取得本周的开始时间
* @return 本周的开始时间
*/
public static Date getThisWeekBeginTime(){
Date beginTime=null;
Calendar cal=Calendar.getInstance();
int week=getWeek(cal.getTime());
week=week-1;
int days=0;
if(week==0){
days=6;
}else{
days=week-1;
}
cal.add(Calendar.DAY_OF_MONTH, -days);
beginTime=cal.getTime();
return beginTime;
}
/**
* 取得本周的开始日期
* @param format 时间的格式
* @return 指定格式的本周最开始时间
*/
public static String getThisWeekBeginTimeString(String format){
DateFormat df=new SimpleDateFormat(format);
return df.format(getThisWeekBeginTime());
}
/**
* 取得本周的结束时间
* @return 本周的结束时间
*/
public static Date getThisWeekEndTime(){
Date endTime=null;
Calendar cal=Calendar.getInstance();
int week=getWeek(cal.getTime());
week=week-1;
int days=0;
if(week!=0){
days=7-week;
}
cal.add(Calendar.DAY_OF_MONTH, days);
endTime=cal.getTime();
return endTime;
}
/**
* 取得本周的结束日期
* @param format 时间的格式
* @return 指定格式的本周结束时间
*/
public static String getThisWeekEndTimeString(String format){
DateFormat df=new SimpleDateFormat(format);
return df.format(getThisWeekEndTime());
}
/**
* 取得两时间相差的天数
* @param from 第一个时间
* @param to 第二个时间
* @return 相差的天数
*/
public static long getBetweenDays(Date from, Date to){
long days=0;
long dayTime=24*60*60*1000;
long fromTime=from.getTime();
long toTime=to.getTime();
long times=Math.abs(fromTime-toTime);
days=times/dayTime;
return days;
}
/**
* 取得两时间相差的小时数
* @param from 第一个时间
* @param to 第二个时间
* @return 相差的小时数
*/
public static long getBetweenHours(Date from,Date to){
long hours=0;
long hourTime=60*60*1000;
long fromTime=from.getTime();
long toTime=to.getTime();
long times=Math.abs(fromTime-toTime);
hours=times/hourTime;
return hours;
}
/**
* 取得在指定时间上加减days天后的时间
* @param date 指定的时间
* @param days 天数,正为加,负为减
* @return 在指定时间上加减days天后的时间
*/
public static Date addDays(Date date,int days){
Date time=null;
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, days);
time=cal.getTime();
return time;
}
/**
* 取得在指定时间上加减months月后的时间
* @param date 指定时间
* @param months 月数,正为加,负为减
* @return 在指定时间上加减months月后的时间
*/
public static Date addMonths(Date date,int months){
Date time=null;
Calendar cal=Calendar.getInstance();
cal.add(Calendar.MONTH, months);
time=cal.getTime();
return time;
}
/**
* 取得在指定时间上加减years年后的时间
* @param date 指定时间
* @param years 年数,正为加,负为减
* @return 在指定时间上加减years年后的时间
*/
public static Date addYears(Date date,int years){
Date time=null;
Calendar cal=Calendar.getInstance();
cal.add(Calendar.YEAR, years);
time=cal.getTime();
return time;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(getFormatDateTime(new Date(), DateUtil.yyyyMMddFormat,"en"));
/* System.out.println(getFormatDateTime(new Date(),DateUtil.fullChineseFormat));
System.out.println(isLeapYear(new Date()));
Calendar cal=Calendar.getInstance();
System.out.println(isInToday(cal.getTime()));
Calendar cal2=Calendar.getInstance();
cal2.set(2011, 06, 05);
System.out.println(isSameDay(cal.getTime(),cal2.getTime()));
System.out.println(WEEKS[getWeek(new Date())]); //星期几
DateFormat df=new SimpleDateFormat(DateUtil.fullFormat);
String fullString="2011-06-03 22:37:20";
try {
Date fulldate=df.parse(fullString);
System.out.println(getBetweenDays(fulldate,cal.getTime()));
System.out.println("ago:"+getAgoTimeString(fulldate));
System.out.println(isWeeks(fulldate));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(getThisWeekBeginTimeString(DateUtil.yyyyMMddChineseFormat));
System.out.println(getThisWeekEndTimeString(DateUtil.yyyyMMddChineseFormat));
System.out.println(addDays(new Date(),3));
System.out.println(addDays(new Date(),-3));
System.out.println(addMonths(new Date(),2));
System.out.println(addMonths(new Date(),-2));
System.out.println(addYears(new Date(),1));
System.out.println(addYears(new Date(),-1));
*/
}
一个处理Date与String的工具类的更多相关文章
- 有意思的String字符工具类
对String的操作是Java攻城师必备的,一个优秀的攻城师是懒惰,他会把自己的一些常见的代码写成可提供拓展和复用的工具类或者工具库,这些是这些优秀工程师的法宝. 我就先从String这个基本操作开始 ...
- String字符串工具类
字符串类(StringUtil.cs) using System; namespace Sam.OA.Common { /// <summary> /// 字符处理工具类 /// 作者:陈 ...
- 自己写了一个解析json为table的工具类
还需要完善的包括,css的封装,触发事件,数据的获得处理: <!DOCTYPE html> <html> <head> <meta charset=" ...
- java格式处理工具类
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- 第13天 JSTL标签、MVC设计模式、BeanUtils工具类
第13天 JSTL标签.MVC设计模式.BeanUtils工具类 目录 1. JSTL的核心标签库使用必须会使用 1 1.1. c:if标签 1 1.2. c:choos ...
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
- Java中使用google.zxing快捷生成二维码(附工具类源码)
移动互联网时代,基于手机端的各种活动扫码和收付款码层出不穷:那我们如何在Java中生成自己想要的二维码呢?下面就来讲讲在Java开发中使用 google.zxing 生成二维码. 一般情况下,Java ...
- Log 日志工具类 保存到文件 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- xml与java代码相互装换的工具类
这是一个java操作xml文件的工具类,最大的亮点在于能够通过工具类直接生成xml同样层次结构的java代码,也就是说,只要你定义好了xml的模板,就能一键生成java代码.省下了自己再使用工具类写代 ...
随机推荐
- legoblock秀上限
很久没有做题了,前天做了一道题结果弱的一逼...搜了解题报告不说...还尼玛秀了上限 题意: 给出宽和高为n和m的一堵墙,手上有长为1,2,3,4高均为1的砖,问形成一个坚固的墙有多少种做法. 坚固的 ...
- Jenkins 四: 启动关闭以及重启jenkins
启动 1. 在桌面新建一个jenkins.bat文件.内容如下: cd /d %JENKINS_HOME% java -jar %JENKINS_HOME%\jenkins.war --httpPor ...
- RabbitMQ-清空队列中(一个channel或连接中)的Unacknowledged状态的消息
清空所有:nack 时将参数delivery-tag设为0,multiple设为1. 清空小于等于某delivery-tag的所有消息:nack 时将参数delivery-tag设为正数(介于1和92 ...
- jquery ajaxform上传文件返回不提示信息的问题
在使用jquery的ajaxform插件进行ajax提交表单并且上传文件的时候,返回类型datatype :json但是后台通过写出一个json对象后,在执行完以后没有进入success函数,而是直接 ...
- Lucene为不同字段指定不同分词器(转)
在lucene使用过程中,如果要对同一IndexWriter中不同 Document,不同Field中使用不同的analyzer,我们该如何实现呢? 通过对<lucene in action&g ...
- java 服务端解决ajax跨域问题
//过滤器方式 可以更改为拦截器方式public class SimpleCORSFilter implements Filter { public void doFilter(ServletRequ ...
- LVS图解 ---阿里
LVS在大规模网络环境中的应用 1. SLB总体架构 LVS本身是开源的,我们对它进行了多方面的改进,并且也已开源-https://github.com/alibaba/LVS. 接下 ...
- RSA体系 c++/java相互进行加签验签--转
在web开发中,采用RSA公钥密钥体系自制ukey,文件证书登陆时,普遍的做法为:在浏览器端采用c++ activex控件,使用 c++的第三库openssl进行RAS加签操作,在服务器端采用java ...
- PHPMailer中文说明
PHPMailer中文说明 A开头: $AltBody --属性出自:PHPMailer ::$AltBody文件:class.phpmailer .php说明:该属性的设置是在邮件正文不支持HTML ...
- [转]Vim 复制粘帖格式错乱问题的解决办法
有时候,复制文本(尤其是代码)到 Vim,会出现格式错乱的问题.看样子,应该是自动缩进惹得祸.本文不去深究原因,直接给出解决方法. 1. paste 模式 运行如下命令,进入 paste 模式: :s ...