日期工具类
来源:互联网 发布:php视频教程下载 编辑:程序博客网 时间:2024/06/05 22:10
package glodon.td.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期处理相关工具类
* @author sunhj-c
*
*/
public class DateUtils {
/**定义常量**/
public static final String DATE_JFP_STR="yyyyMM";
public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FULL_STR_SSS = "yyyy-MM-dd HH:mm:ss SSS";
public static final String DATE_SMALL_STR = "yyyy-MM-dd";
public static final String DATE_KEY_STR = "yyMMddHHmmss";
public static final String DATE_KEY_STR_SSS = "yyyyMMddHHmmssSSS";
/**
* 使用预设格式提取字符串日期
* @param strDate 日期字符串
* @return
*/
public static Date parse(String strDate) {
return parse(strDate,DATE_FULL_STR);
}
/**
* 使用用户格式提取字符串日期
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 将LONG时间转化为Date
* @param times
* @return
*/
public static String parseLongToDate(long times) {
// 计算毫秒
long mm = times % 1000;
long fg=times*1;
if (fg < 0) {
mm = 0;
return "" + mm + "天" + mm + "时" + mm + "分" + mm + "秒";
// return "" + mm + "天" + mm + "时" + mm + "分" + mm + "秒" + mm + "毫秒";
}
//秒
long x = (times - mm) / 1000;
//计算秒数
long s = x % 60;
//分钟
x = (x - s)/60;
long mi = x % 60;
//小时
x = (x - mi) / 60;
long h = x % 24;
// 天
x = (x - h) / 24;
long d = x;
String tString = "" + d + "天" + h + "时" + mi + "分" + s + "秒";
// String tString = "" + d + "天" + h + "时" + mi + "分" + s + "秒" + mm
// + "毫秒";
return tString;
}
/**
* 获取系统当前时间
* @return
*/
public static String getNowTime(String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format(new Date());
}
/**
* 格式化指定时间
* @return
*/
public static String getNowTime(String type,Date time) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format(time);
}
/**
* 获取系统当前时间
* @return
*/
public static String getDateTime(Date time,String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format(time);
}
/**
* 获取系统当前时间
* @return
*/
public static Date getNowTime() {
return new Date();
}
/**
* 延迟某个时间
* @param begin
* @param delayType
* @param number
* @return
*/
public static Date getDelayTime(Date beginTime,int delayType,int number)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginTime);
calendar.add(delayType, number);
return calendar.getTime();
}
/**
* 将时间转换成字符串
* @param date 日期
* @return
*/
public static String getDateString(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FULL_STR);
return dateFormat.format(date);
}
public static void main(String[] args) {
String s=parseLongToDate(-10000);
System.out.println(s);
}
}
- 工具类-日期工具类
- 日期工具类
- 日期工具类
- 日期工具类
- java日期工具类
- 日期工具类
- 日期工具类
- 一个日期工具类
- 日期工具类
- java日期工具类
- flex 日期工具类
- 工具类:日期处理
- java 日期工具类
- 日期工具类
- 日期工具类DataTool
- Java日期工具类
- 日期工具类TimeUnit
- DateTool日期工具类
- linux fork函数与vfork函数
- c++共享锁的使用
- ./configure && make && make install
- [leetcode] 289. Game of Life
- PHP中的关键判断(boolean,empty,isset)
- 日期工具类
- IntelliJ IDEA 快捷输入模板
- R在开放数据的应用
- std::vector
- 重要文件
- Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision
- MindMapper中的默认样式怎样进行修改
- 根据Excel单元格坐标修改单元格内的Value
- 加密算法比较3DES AES RSA ECC MD5 SHA1等