`

一个时间转换的工具类

    博客分类:
  • java
阅读更多
package com.founder.cissservice.util;

import java.text.*;
import java.util.Calendar;
import java.util.Date;

public class FormatDate {
	/**
	 * 根据日历时间提取yyyy-mm-dd格式的时间
	 * 
	 * @param Date
	 * @return String 2002-12-13 Format
	 */
	public static String toY_M_D(Date date) throws RuntimeException {
		return toSimpleDateFormat(date, "yyyy-MM-dd");
	}

	/**
	 * 根据日历时间提取yyyymmdd格式的时间
	 * 
	 * @param Date
	 * @return String 20021213 Format
	 */
	public static String toYMD(Date date) throws RuntimeException {
		return toSimpleDateFormat(date, "yyyyMMdd");
	}
	
	
	public static String toCN_Y_M_D(Date date) throws RuntimeException {
		return toSimpleDateFormat(date, "yyyy年MM月dd日");
	}

	/**
	 * 返回yyyy-mm-dd hh:mm:ss格式的时间
	 * 
	 * @param date
	 * @return
	 */
	public static String toY_M_D_H_M_S(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm:ss");
	}

	public static String toCN_Y_M_D_H_M_S(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy年MM月dd日 HH时mm分ss秒");
	}

	/**
	 * 返回yyyy-mm-dd hh:mm格式的时间
	 * 
	 * @param date
	 * @return
	 */
	public static String toY_M_D_H_M(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm");
	}

	public static String toCN_Y_M_D_H_M(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy年MM月dd日 HH时mm分");
	}

	/**
	 * 根据时间返回hh:mm:ss格式的字符串
	 * 
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static String toH_M_S(Date time) throws RuntimeException {
		if (time == null) {
			return null;
		} else {
			Calendar cd = Calendar.getInstance();
			cd.setTime(time);
			return cd.get(Calendar.HOUR_OF_DAY)+":"+cd.get(Calendar.MINUTE)+":"+cd.get(Calendar.SECOND);
//			return time.getHours() + ":" + time.getMinutes() + ":"
//					+ time.getSeconds();
		}
	}

	/**
	 * 字符串格式转换称日期格式
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date fromY_M_D(String date) throws RuntimeException {
		return fromSimpleDateFormat(date, "yyyy-MM-dd");
	}

	/**
	 * 日期时间格式
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date fromY_M_D_H_M_S(String dateTime) throws RuntimeException {
		return fromSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 根据时分秒装还称时间
	 * 
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static Date fromH_M_S(String time) throws RuntimeException {
		try {
			return fromSimpleDateFormat(time, "HH:mm:ss");
		} catch (Exception e) {
			return fromSimpleDateFormat(time, "yyyy-MM-dd HH:mm:ss");
		}
	}

	/**
	 * 得到当前的时间
	 * 
	 * @return
	 */
	public static Date getCurrDate() {
		return new Date();
	}

	/**
	 * 统一的时间构造方法
	 * 
	 * @param date
	 * @param simpleDateFormat
	 * @return
	 */
	public static String toSimpleDateFormat(Date date, String simpleDateFormat) {
		SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
		if (date == null) {
			return null;
		} else {
			return format.format(date);
		}
	}

	/**
	 * 简单时间格式
	 * 
	 * @param date
	 * @param simpleDateFormat
	 * @return
	 */
	public static Date fromSimpleDateFormat(String date, String simpleDateFormat)
			throws RuntimeException {
		SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
		if (date == null || date.equals("")) {
			return null;
		} else {
			try {
				return format.parse(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	}
}

 

分享到:
评论

相关推荐

    常用时间转换工具类

    Android开发过程中经常会涉及到时间转换的问题,此工具类包含了常用的时间转换方法。可直接用于开发中,方便快捷,节省开发时间。通用性强。

    Java - DateUtil 日期时间转换工具类

    资源名称:DateUtil 日期时间转换工具类 内容概要:日期时间转换工具类,包括基本的Date类型,String类型,TimeStamp类型,LocalDateTime类型,LocalDate类型之间的互相转换,还提供了许多与时间获取,时间计算有关...

    日期转换工具类

    ### 日期转换工具类 #### 知识点概述 本文将详细介绍一个名为`DateTimeUtil`的Java工具类,该类提供了日期格式化和解析的功能。此类利用了`org.joda.time`包中的`DateTime`和`DateTimeFormat`类来实现字符串与日期...

    时间转换工具类

    秒 HH:mm:ss 2、将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss 将短时间格式时间转换为字符串 yyyy-MM-dd 将短时间格式字符串转换为时间 yyyy-MM-dd 将短...

    GMT格式与普通时间格式转换工具类,可直接使用;附带普通时间类型字符串和时间互相转换的可直接使用的接口

    总之,`DateFormatUtil`工具类是处理日期时间转换的一个实用工具,它通过提供简单易用的方法,帮助开发者在GMT和本地时间格式之间自由切换,并且支持字符串与日期时间对象的相互转换。无论是在传统的Java项目还是...

    长整型时间转换工具

    在“时间转换工具.exe”这个文件中,我们可以推测这是一个Windows可执行程序,它实现了上述功能。用户可以输入长整型时间戳,或者输入日期和时间,然后得到相应的转换结果。由于没有具体程序的详细信息,我们无法...

    小程序日期转换的工具类

    此工具类包含了日期和时间戳互转,根据生日计算出年龄和星座

    时间、IP转换工具

    时间转换工具有助于在这些时间格式之间进行转换,例如,从用户输入的本地时间转换为UTC,或将UTC时间戳转换为可读的日期和时间。此外,工具可能还会提供时区转换功能,以便用户了解不同地区的时间。 其次,我们讨论...

    时间格式转换工具类

    时间格式转换,提供丰富的时间格式转换方法

    java日期转换工具类

    日期格式转字符串格式,日期加上天数,新日期,两个时间相差距离多少天

    时间日期转换 工具类

    时间日期转换工具类!各种格式各种转换和一些计算 交易日的工具

    vopo转换工具类及所需jar

    "vopo转换工具类及所需jar"是一个特定的项目,它提供了一个简单的方法来处理名为"vopo"的数据格式之间的转换。这个工具包可能包含了一些核心的类和依赖的库,使得开发者能够便捷地实现这种转换操作。 首先,`...

    标准时间UTC相互转换工具

    标题中的“标准时间UTC相互转换工具”指的是一个应用程序,它能帮助用户在协调世界时间(UTC)和本地标准时间之间进行转换。协调世界时间是一种国际上广泛采用的时间标准,主要用于科学、航空、航海以及全球通信等...

    vue 字符和日期转换工具类

    vue 字符和日期转换工具类,先将util里面的公共对象抛出,全局使用

    json工具类,java日期转换,字符串转换等各种工具类

    Java 8的`java.time`包提供了丰富的日期时间API,如`Period`和`Duration`用于计算两个日期或时间差,`TemporalAdjusters`允许进行复杂的日期调整,如下一个工作日等。 这些工具类是提高开发效率的关键,它们简化了...

    时间转换工具类TimeUtil.java

    实现了对String 日期型(例2019-6-21),long型时间戳(如1561083887098(对应2019-6-21上午十点二十多分)),Date型,Calender型的四种日期格式相互转换。测试机型android 6.0,8.0,8.1,9.0。对应部分String型...

    一个非常好的Java工具类(字符窜处理,时间格式转换,加密解密等)

    一个非常好的Java工具类 包括字符窜加密解密,非法字符过滤和替换 时间格式转换,常用验证等等很多非常有用的函数

Global site tag (gtag.js) - Google Analytics