DateFormatUtil.java
package com.vcredit.framework.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
/**
* @ClassName: DateFormatUtil
* @Description:与Date相关的公用类
* @author dk
* @date 2014年12月5日 下午2:29:31
*
*/
public class DateFormatUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final String ENG_DATE_FROMAT = "EEE, d MMM yyyy HH:mm:ss z";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String MM_DD_HH_MM = "MM-dd HH:mm";
public static final String MM_DD_HH_MM_CN = "MM月dd日 HH:mm";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY = "yyyy";
public static final String MM = "MM";
public static final String DD = "dd";
/**
* @Title: dateToString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param date
* @param @param formate
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String dateToString(Date date, String formate) {
if (date == null) {
return "";
}
DateFormat dateFormat = new SimpleDateFormat(formate);
return dateFormat.format(date);
}
public static String dateToString(Date date) {
if (date == null) {
return "";
}
DateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
return dateFormat.format(date);
}
public static String datetime2String(Date date) {
if (date == null) {
return "";
}
return sdf.format(date);
}
/**
* 得到本周周一
*
* @return yyyy-MM-dd
*/
public static String getMondayOfThisWeek() {
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(c.getTime());
}
/**
* 得到本周周日
*
* @return yyyy-MM-dd
*/
public static String getSundayOfThisWeek() {
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 7);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(c.getTime());
}
/**
* @描述 —— 格式化日期对象
*/
public static Date date2date(Date date, String formatStr) {
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
String str = sdf.format(date);
try {
date = sdf.parse(str);
} catch (Exception e) {
return null;
}
return date;
}
/**
* @描述 —— sql时间对象转换成字符串
*/
public static String timestamp2string(Timestamp timestamp, String formatStr) {
String strDate = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
strDate = sdf.format(timestamp);
return strDate;
}
/**
* @描述 —— 字符串转换成时间对象
*/
public static Date string2date(String dateString, String formatStr) {
if (StringUtils.isBlank(dateString)) {
return null;
}
Date formateDate = null;
DateFormat format = new SimpleDateFormat(formatStr);
try {
formateDate = format.parse(dateString);
} catch (ParseException e) {
return null;
}
return formateDate;
}
/**
* @描述 —— Date类型转换为Timestamp类型
*/
public static Timestamp date2timestamp(Date date) {
if (date == null)
return null;
return new Timestamp(date.getTime());
}
/**
* @描述 —— 获得当前年份
*/
public static String getNowYear() {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
return sdf.format(new Date());
}
/**
* @描述 —— 获得当前月份
*/
public static String getNowMonth() {
SimpleDateFormat sdf = new SimpleDateFormat(MM);
return sdf.format(new Date());
}
/**
* @描述 —— 获得当前日期中的日
*/
public static String getNowDay() {
SimpleDateFormat sdf = new SimpleDateFormat(DD);
return sdf.format(new Date());
}
/**
* @描述 —— 指定时间距离当前时间的中文信息
*/
public static String getLnow(long time) {
Calendar cal = Calendar.getInstance();
long timel = cal.getTimeInMillis() - time;
if (timel / 1000 < 60) {
return "1分钟以内";
} else if (timel / 1000 / 60 < 60) {
return timel / 1000 / 60 + "分钟前";
} else if (timel / 1000 / 60 / 60 < 24) {
return timel / 1000 / 60 / 60 + "小时前";
} else {
return timel / 1000 / 60 / 60 / 24 + "天前";
}
}
/**
* 当前时间一星期前的时间
*
* @return
*/
public static final String getLastWeek() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -7);
return sdf.format(calendar.getTime());
}
/**
* 获取当前日期的指定格式的字符串
*
* @param format
* 指定的日期时间格式,若为null或""则使用指定的格式"yyyy-MM-dd HH:mm"
* @return
*/
public static String getCurrentTime(String format) {
if (format == null || format.trim().equals("")) {
sdf.applyPattern(YYYY_MM_DD_HH_MM_SS);
} else {
sdf.applyPattern(format);
}
return sdf.format(new Date());
}
public static String getCurrentDate() {
return dateToString(new Date(), YYYY_MM_DD_HH_MM_SS);
}
public static Boolean checkIsTime(String time) {
DateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
try {
format.parse(time);
} catch (ParseException e) {
return false;
}
return true;
}
/**
* 比较日期先后
*
* @param date
* @param interval
* @return boolean
*/
public static boolean isExpired(Date date, int interval) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, interval);
Calendar now = Calendar.getInstance();
return calendar.before(now);
}
public static Date string2date(String value) {
if (StringUtils.isBlank(value)) {
return null;
}
return string2date(value, "yyyy-MM-dd");
}
public static String string2string(String value, String from, String to) {
return dateToString(string2date(value, from), to);
}
/**
* 加1天
*
* @param value
* @return Date
*/
public static Date addOneDay(String value) {
return addDays(value, 1);
}
public static Date addOneDay(Date value) {
return addDays(value, 1);
}
public static Date addDays(String value, int num) {
return addDays(string2date(value), num);
}
public static Date addDays(Date value, int num) {
Calendar c = Calendar.getInstance();
c.setTime(value);
c.add(Calendar.DATE, num);
return c.getTime();
}
public static Date addMonth(Date value, int num) {
Calendar c = Calendar.getInstance();
c.setTime(value);
c.add(Calendar.MONTH, num);
return c.getTime();
}
public static int getBetweenTwoDays(Date start, Date end) {
if (start == null) {
return 0;
}
if (end == null) {
end = new Date();
}
long longs = (end.getTime() - start.getTime());
int days = (int) (longs / (24 * 60 * 60 * 1000));
return days;
}
public static int getBetweenTwoDays(String start, String end) {
return getBetweenTwoDays(string2date(start), string2date(end));
}
/**
* 获取当前日期(只精确到天)
*/
public static Date getNow() {
return date2date(new Date(), "yyyy-MM-dd");
}
/**
* 获取明天日期(精确到天)
*/
public static Date getTomorrow() {
return addOneDay(getNow());
}
public static String now() {
return datetime2String(getNow());
}
public static Date getLastMonth() {
return addMonth(getNow(), -1);
}
public static Date getNextMonth() {
return addMonth(getNow(), 1);
}
public static Integer getAge(String identityNo) {
String birthDay = CommonUtils.getIdentityBirth(identityNo);
int days = getBetweenTwoDays(string2date(birthDay, "yyyy-MM-dd"), new Date());
return days / 365;
}
/**
* 根据年,月 得到指定月的第一天
* @param year
* @param month
* @return
*/
public static String getFirstDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
}
DateFormatUtil.java的更多相关文章
- 找出Java进程中大量消耗CPU
原文:https://github.com/oldratlee/useful-shells useful-shells 把平时有用的手动操作做成脚本,这样可以便捷的使用. show-busy-java ...
- Java相关脚本
本人摘自:https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-thread ...
- 用于快速排查Java的CPU性能问题(top us值过高)
转载于GIT路径 https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-th ...
- Solr部分更新MultiValued的Date日期字段时报错及解决方案
问题描述如标题. 异常信息如下: Result Caused by: org.apache.solr.common.SolrException: Invalid Date String:'Mon Se ...
- Solr部分更新MultiValued的Date日期字段时报错及解决方案:Invalid Date String:'Mon Sep 14 01:48:38 CST 2015'
问题描述如标题. 异常信息如下: Result Caused by: org.apache.solr.common.SolrException: Invalid Date String:'Mon Se ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java POI 解析word文档
实现步骤: 1.poi实现word转html 2.模型化解析html 3.html转Map数组 Map数组(数组的操作处理不做说明) 1.导jar包. 2.代码实现 package com.web.o ...
- CommonUtils.java
package com.vcredit.framework.utils; import java.lang.reflect.InvocationTargetException;import java. ...
- java计算某个日期是什么节气(24节气)
package com.test; import java.util.Calendar; import java.util.Date; /** * Created by json */ public ...
随机推荐
- Android性能优化之布局优化
最新最准确内容建议直接访问原文:Android性能优化之布局优化 本文为Android性能优化的第二篇——布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不 ...
- Android 使用 LruCache 缓存图片
在你应用程序的 UI 界面加载一张图片是一件很简单的事情,但是当你需要在界面上加载一大堆图片的时候,情况就变得复杂起来.在很多情况下,(比如使用 ListView, GridView 或者 ViewP ...
- JQuery学习之jQuery尺寸
1.width()和height()方法: width():设置或返回元素的宽度(不包括内边距,边框或外边距) height():设置或返回元素的高度(不包括内边距,边框或外边距) $("b ...
- Asp.net mvc 有关序列化的问题
// $.ajax({// async: false,// type:'post',//这里注意,Get请求不安全,用 ...
- http://www.roncoo.com/course/view/a09d8badbce04bd380f56034f8e68be0
http://www.roncoo.com/course/view/a09d8badbce04bd380f56034f8e68be0
- bash脚本中的普通数组和关联数组
1. 普通数组 bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0. ...
- mysql 如何判断 "字符串" 是否为 "数字"
这个问题有点怪 ,但很多时候我们会以字符串的形式存储数字 , 反过来我们用字符串进行数学运算时, 好像也不会出错 . 除非 , 用作数学运算的字符串不能转换成数字 .但是我们改如何判断字符串是否能转换 ...
- 使用CSS/JS代码修改博客模板plus
之前对CSS/JavaScript了解还不深,只是把模板的CSS胡乱修改了几个属性.最近正好也在做一个网站的前端,学习了不少东西,再来改一改~ 上次最后之所以铩羽而归,是因为从CSS里找不到那些#和. ...
- python 代码片段24
#coding=utf-8 #内部类 class MyClass(object): class InnerClass: pass # 正则表达式 # 通过re模块来访问 import re m=re. ...
- jquery 动画效果插件
从jQuery API 文档中可以知道,jQuery自定义动画的函数.animate( properties [, duration] [, easing] [, complete] )有四个参数: ...