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的更多相关文章

  1. 找出Java进程中大量消耗CPU

    原文:https://github.com/oldratlee/useful-shells useful-shells 把平时有用的手动操作做成脚本,这样可以便捷的使用. show-busy-java ...

  2. Java相关脚本

    本人摘自:https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-thread ...

  3. 用于快速排查Java的CPU性能问题(top us值过高)

    转载于GIT路径 https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-th ...

  4. Solr部分更新MultiValued的Date日期字段时报错及解决方案

    问题描述如标题. 异常信息如下: Result Caused by: org.apache.solr.common.SolrException: Invalid Date String:'Mon Se ...

  5. 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 ...

  6. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  7. Java POI 解析word文档

    实现步骤: 1.poi实现word转html 2.模型化解析html 3.html转Map数组 Map数组(数组的操作处理不做说明) 1.导jar包. 2.代码实现 package com.web.o ...

  8. CommonUtils.java

    package com.vcredit.framework.utils; import java.lang.reflect.InvocationTargetException;import java. ...

  9. java计算某个日期是什么节气(24节气)

    package com.test; import java.util.Calendar; import java.util.Date; /** * Created by json */ public ...

随机推荐

  1. bpl 包的编写和引用

    转载:http://www.cnblogs.com/gxch/archive/2011/04/23/bpl.html 为什么要使用包? 答案很简单:因为包的功能强大.设计期包(design-time ...

  2. C中头文件在cpp文件中引用和.h文件引用

    1.编译器会单独编译每个cpp文件.头文件会复制到cpp文件中. 2.有时会遇到这样的一个问题a.cpp要调用b.cpp中的函数,而b.cpp又要调用a.cpp中的函数.这就牵扯到相互调用.这时如果我 ...

  3. three.js入门3

    为什么要用three.js Three.js为我们封装了底层的WebGl接口,使我们在无需掌握繁冗的图形学知识的基础下可以轻松的创建三维场景.相比较使用底层的WebGL我们可以使用更少的代码,大大的降 ...

  4. java中 == 与 equal 的区别

    http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452156.html String str1 = new String("s ...

  5. background-attachment 定义背景图片随滚动轴的移动方式

  6. 为OpenResty增加nginx_upload_module模块

    1.下载 http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz 或 http://pan.baidu.com/s ...

  7. leetcode Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  8. ACM 红黑树

    红黑树 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 什么是红黑树呢?顾名思义,跟枣树类似,红黑树是一种叶子是黑色果子是红色的树... 当然,这个是我说的... & ...

  9. ACM: POJ 3660 Cow Contest - Floyd算法

    链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Descri ...

  10. Using Maven to generate a Java Project or Web project

    I often to generate a Java project or Web project with Eclipse tool. Well, I have no idea when I wan ...