日期工具类
2015-11-26 11:30
645 查看
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);
}
}
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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统