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 ...
随机推荐
- RecyclerView 介绍 01
RecyclerView是Android support v7里面是一个自定义控件.用来显示大量数据集合.类似ListView和GridView这两个控件,RecyclerView同样可以实现,甚至更 ...
- 在html 中嵌入优酷视频
<html> <embed src="http://player.youku.com/player.php/sid/XMjAzOTk4NjI4/v.swf" qu ...
- BestCoder Round #71 (div.2)
数学 1001 KK's Steel 类似斐波那契求和 #include <cstdio> #include <cstring> #include <algorithm& ...
- memcached启动脚本以及telnet测试
memcached -m 1024 -u root -p 11211 -c 1024 -P /var/memcached.pid -d memcached 1.2.2 -p <num> T ...
- 【原】iOS学习18之OC内存管理高级
1.属性的内存管理 1> 属性的语义特性 2> assign下的属性内部实现 @property (nonatomic, assign) NSString *name; @synthesi ...
- BZOJ2159 : Crash 的文明世界
$x^k=\sum_{i=1}^k Stirling2(k,i)\times i!\times C(x,i)$ 设$f[i][j]=\sum_{k=1}^n C(dist(i,k),j)$. 则可以利 ...
- Colorado Potato Beetle(CF的某道) & 鬼畜宽搜
题意: 一个人在一张大图上走,给你路径与起点,求他走出的矩形面积并.(大概这个意思自行百度标题... SOL: 与其说这是一道图论题不如说是一道生动活泼的STL-vector教学.... 离散化宽搜, ...
- android布局详解
http://blog.163.com/zhangzheming_282/blog/static/117920962013072502787/ AbsoluteLayout——绝对布局 必 ...
- 使用CSS3的appearance属性改变元素的外观
昨天在和同事一起完成项目的时候,我使用了appearance来渲染select,但是在firefox下出现问题,不完美,最后去除了.但还是要学习下这个属性.大家都知道每个浏览器对HTML元素渲染都不一 ...
- [Java] 利用LinkedHashMap来实现LRU Cache
http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html https://oj.leetcode.com/discuss/1188/java- ...