public class DateFormatUtils {
private static Log logger = LogFactory.getLog(DateFormatUtils.class); public static String formatDate(String formater,Date date){
SimpleDateFormatformate = new SimpleDateFormat(formater);
formate.format(date);
return formate.format(date);
} /**
*
* @Title:formatDateToCommon
* @Description: 通用时间转换类型
* @param date
* @return
*/
public static String formatDateToCommon(Date date){
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formate.format(date);
} /**
*
* @Title:getSystemDate
* @Description: 获取系统当前时间
* @param date
* @return
* @throws Exception
*/
public static Date getSystemDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(sdf.format(new Date()));
} catch (ParseException e) {
logger.error("", e);
}
return null ;
} /**
*
* @Title:SystemDateFormatToCommon
* @Description: 获取系统当前时间
* @return
*/
public static String getSystemDateFormatToCommon(){
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formate.format(new Date());
} public static String getSystemDateFormatToYYYYMMDD(){
SimpleDateFormat formate = new SimpleDateFormat("yyyyMMdd");
return formate.format(new Date());
} public static String getSystemDateFormatToYYYYMMDDHHmmss(){
SimpleDateFormat formate = new SimpleDateFormat("yyyyMMddHHmmss");
return formate.format(new Date());
} /**
*
* @Title:getFormatDateCommon
* @Description: 格式化时间
* @param date
* @return
* @throws Exception
*/
public static Date getFormatDateCommon(Date date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(sdf.format(date));
} catch (ParseException e) {
logger.error("", e);
}
return null;
} /**
*
* @Title:StringToDate
* @Description: 字符串转换成日期
* @param dateStr
* @param formatStr
* @return
* @throws ParseException
*/
public static Date StringToDate(String dateStr) throws ParseException {
DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=null;
date = sdf.parse(dateStr);
return date;
} public static Date StringToDate(String dateStr, String pattern){
try{
DateFormat sdf=new SimpleDateFormat(pattern);
Date date = sdf.parse(dateStr);
return date;
}catch(ParseException ex){
return null;
}
} /**
*
* @Title:fromDateStringToLong
* @Description: 获取字符串时间格式的毫秒数
* @param inVal
* @return
*/
public static long fromDateStringToLong(String inVal) {
return fromDateStringToLong(inVal, "yyyy-MM-dd HH:mm:ss");
}
public static long fromDateStringToLong(String inVal,String format) {
Date date = null; // 定义时间类型
SimpleDateFormat inputFormat = new SimpleDateFormat(format);
try {
date = inputFormat.parse(inVal); // 将字符型转换成日期型
} catch (Exception e) {
logger.error("", e);
}
return date.getTime(); // 返回毫秒数
} /**
*
* @Title:getMillForDateTimeDouble
* @Description: 获取两个时间之间的毫秒数
* @param inVal
* @return
*/
public static long getMillForDateTimeDouble(Date startTime, Date endTime) {
long lTime = startTime.getTime();
long eTime = endTime.getTime();
long s = eTime - lTime ;
return s;
} /**
*
* @Title:formatDuring
* @Description: 将毫秒数转换为时分秒
* @param mss
* @return
*/
public static String formatDuring(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
return days + " 天 " + hours + " 时 " + minutes + " 分 "
+ seconds + " 秒 ";
} /**
* 计算两个日期之间相差的天数
* @param format yyyyMMdd
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(String format,Date smdate,Date bdate) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat(format);
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days));
} /**
* 计算两个日期之间相差的天数
* 字符串的日期格式的计算
* @param format yyyyMMdd
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(String format,String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat(format);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days));
} /**
*
* @Title:getSystemAddMinute
* @Description: 获取当前系统时间毫秒数 + n分钟后的时间
* @param currentTimeMillis 当前系统时间毫秒数
* @param minute 分
* @return
*/
public static String getSystemByCurrentTimeMillisAndMinute(long currentTimeMillis, int minute){
long currentTime = System.currentTimeMillis() + minute * 60 * 1000;
Date date = new Date(currentTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
} /**
*
* @Title:getDateStrByTimeMillis
* @Description: 以字符串形式根据毫秒数获取时间
* @param currentTimeMillis
* @return
*/
public static String getDateStrByTimeMillis(long currentTimeMillis){
Date date = new Date(currentTimeMillis);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
} /**
*
* @Title:getDateStrByTimeMillis
* @Description: 以字符串形式根据毫秒数获取时间
* @param currentTimeMillis
* @return
*/
public static Date getDateByTimeMillis(long currentTimeMillis){
Date date = new Date(currentTimeMillis);
return date;
} /****
* 传入具体日期 ,返回具体日期减一个月。
*
* @param date
* 日期(2014-04-20)
* @return 2014-03-20
* @throws ParseException
*/
public static String addMonth(String yearMonth) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = sdf.parse(yearMonth);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt); rightNow.add(Calendar.MONTH, +1);
Date dt1 = rightNow.getTime();
String reStr = sdf.format(dt1); return reStr;
} /**
*
* @param dateStr 传入的日期字符串
* @param formater 对传入日期和传出日期的格式化类型
* @param monthCount 增加月份传入正值,减去月份传入负值
* @return
* @throws ParseException
*/
public static String calMonth(String dateStr, String formater, int monthCount) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(formater);
Date dt = sdf.parse(dateStr);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt); rightNow.add(Calendar.MONTH, monthCount);
Date dt1 = rightNow.getTime();
String reStr = sdf.format(dt1);
return reStr;
}
}

java日期格式转换大全的更多相关文章

  1. Java日期格式转换

    Java时间格式转换大全 import java.text.*;import java.util.Calendar;public class VeDate {/**   * 获取现在时间   *    ...

  2. java日期格式转换工具类

    原文地址:http://blog.csdn.net/zhiweianran/article/details/7991531 package com.ace.backoffice.utils; impo ...

  3. Java日期格式转换不用发愁

    前言 Java 中日期.时间相关的类相当的多,并且分不同的版本提供了不同的实现,包括 Date . Calendar . LocalDateTime . ZoneDateTime . OffsetDa ...

  4. JAVA时间格式转换大全

    import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 ...

  5. C#日期格式转换大全

    有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6-6或更多的该怎么办呢 我们要用到:DateTim ...

  6. C#常用日期格式处理转换[C#日期格式转换大全

    DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2.Text = dt.ToFile ...

  7. JAVA日期格式转换---让人不得不说的故事

    链接:https://my.oschina.net/xinxingegeya/blog/394821 这是给我自己参考的,大家不惜勿喷 1.举例使用 2.各种作用 3.坑(默认中文日期,加上这个就是英 ...

  8. java 日期格式转换EEE MMM dd HH:mm:ss z yyyy

    SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale. ...

  9. Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

随机推荐

  1. python + redis +ipset实现IP黑名单的动态添加及解封禁

    1.抽空用python做了一个 动态添加/删除IP黑名单 的程序(或者说实现方案),项目地址: https://gitee.com/lowmanisbusy/ip_blacklists, 2.这里的实 ...

  2. idea中隐藏.iml文件

    在创建父子工程或者聚合工程时产生的大量 .iml 文件,有时会对我们的操作产生干扰,所以,一般情况下,我们都将其隐藏掉,步骤如下: File——>settings——>Editor——&g ...

  3. linux MySQL5.7 rpm安装(转)

    删除旧包: # rpm -qa | grep -i mysql # rpm -ev mysql-libs-* --nodeps 安装rpm包: # rpm -ivh mysql-community-c ...

  4. clr调试扩展和DAC

    SOS.DLL.SOSEX.DLL这两个就是用来对.NET程序在Windows调试工具中起到翻译作用的调试器扩展.简单讲就是,这两个组件是.NET项目组专门开发出来用来对.NET应用程序进行方便调试用 ...

  5. random库

    伪随机数的原因: random库中函数主要用于产生各种分布的伪随机数序列.random库中的随机函数是按照一定算法模拟产生的,其概率是确定的.可见的,所以被称为伪随机数.而真正意义上的随机数是按照实验 ...

  6. Micro Benchmark Framework java 基准测试类库

    Micro Benchmark Framework 框架主要是method 层面上的 benchmark,精度可以精确到微秒级 比较典型的使用场景还有: 想定量地知道某个函数需要执行多长时间,以及执行 ...

  7. benchmarkdotnet dotnet 基准测试类库试用(一)

    使用基准测试对于我们应用的性能优化是比较好的方式,可以快速看出优化的结果同时可以给出报告结果 benchmarkdotnet 是dotnet 版本的一个工具,以下是一个简单的试用 环境准备 我使用的是 ...

  8. 为什么vue组件中的data不是一个对象而是一个函数

    如果两个实例引用同一个对象,当其中一个实例的属性发生改变时,另一个实例属性也随之改变,只有当两个实例拥有自己的作用域时,才不会相互干扰. 这是因为JavaScript的特性所导致,在component ...

  9. 第4组 Alpha冲刺(1/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.安排好各个组员的任务 2.收集各个组员的进度 3.写页面 4.写博客 展示Gi ...

  10. 【XR-4】题

    题面 题解 由题,所求为方程\(y^2 = x^2 + ax + b\)的整数解数量. 两边同乘\(4\),可得\((2y)^2 = 4x^2 + 4ax + 4b\). 配方后得\((2y)^2 = ...