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 ...
随机推荐
- Redis 的 5 个常见使用场景
2015-07-22 23:31:46 本文由 伯乐在线 - 刘晓鹏 翻译,黄利民 校稿.未经许可,禁止转载!英文出处:Joe Engel.欢迎加入翻译组. 在这篇文章中,我们将阐述 Redis 最常 ...
- NSDictionary、NSMutableDictionary及其枚举器的用法
.不可变词典NSDictionary 字典初始化 NSNumber *numObj = [NSNumber numberWithInt:]; 以一个元素初始化 NSDictionary *dic = ...
- javaScript入门第一天
JavaScript提供七种不同的data types(数据类型),它们是undefined(未定义), null(空), boolean(布尔型), string(字符串), symbol(符号), ...
- 在windows和linux下如何查看80端口占用情况?是被哪个进程占用?如何终止等
一.在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等 这里主要是用到windows下的DOS工具,点击"开始"--"运行",输入&quo ...
- PL/SQL 循环结构
(1)LOOP...EXIT...END语句示例: control_var:; LOOP then EXIT; END IF; control_var:; END LOOP; 上述,初始化contro ...
- 【原】iOS学习之在NSObject子类中获取当前屏幕显示的ViewController
我们在非视图类中想要随时展示一个view时,需要将被展示的view加到当前view的子视图,或用当前view presentViewController,或pushViewContrller,这些操作 ...
- ACM 无线网络覆盖
无线网络覆盖 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们的乐乐同学对于网络可算得上是情有独钟,他有一个计划,那就是用无线网覆盖郑州大学. 现在学校给了他一个 ...
- 洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- Jquery表格变色 复选框全选,反选
/*jquery静态表格变色*/ $(".tr2").mouseover(function(){ $(this).css("background"," ...
- BZOJ 1003 物流运输 题解 【SPFA+DP】
BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...