日期工具类DateUtils
2016-05-16 11:38
507 查看
[java] view
plain copy
package date.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
static final String formatPattern = "yyyy-MM-dd";
static final String formatPattern_Short = "yyyyMMdd";
/**
* 获取当前日期
* @return
*/
public static String getCurrentDate(){
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
return format.format(new Date());
}
/**
* 获取制定毫秒数之前的日期
* @param timeDiff
* @return
*/
public static String getDesignatedDate(long timeDiff){
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
long nowTime = System.currentTimeMillis();
long designTime = nowTime - timeDiff;
return format.format(designTime);
}
/**
*
* 获取前几天的日期
*/
public static String getPrefixDate(String count){
Calendar cal = Calendar.getInstance();
int day = 0-Integer.parseInt(count);
cal.add(Calendar.DATE,day); // int amount 代表天数
Date datNew = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
return format.format(datNew);
}
/**
* 日期转换成字符串
* @param date
* @return
*/
public static String dateToString(Date date){
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
return format.format(date);
}
/**
* 字符串转换日期
* @param str
* @return
*/
public static Date stringToDate(String str){
//str = " 2008-07-10 19:20:00 " 格式
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
if(!str.equals("")&&str!=null){
try {
return format.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
//java中怎样计算两个时间如:“21:57”和“08:20”相差的分钟数、小时数 java计算两个时间差小时 分钟 秒 .
public void timeSubtract(){
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date begin = null;
Date end = null;
try {
begin = dfs.parse("2004-01-02 11:30:24");
end = dfs.parse("2004-03-26 13:31:40");
} catch (ParseException e) {
e.printStackTrace();
}
long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
long day1 = between / (24 * 3600);
long hour1 = between % (24 * 3600) / 3600;
long minute1 = between % 3600 / 60;
long second1 = between % 60;
System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分"
+ second1 + "秒");
}
}
plain copy
package date.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
static final String formatPattern = "yyyy-MM-dd";
static final String formatPattern_Short = "yyyyMMdd";
/**
* 获取当前日期
* @return
*/
public static String getCurrentDate(){
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
return format.format(new Date());
}
/**
* 获取制定毫秒数之前的日期
* @param timeDiff
* @return
*/
public static String getDesignatedDate(long timeDiff){
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
long nowTime = System.currentTimeMillis();
long designTime = nowTime - timeDiff;
return format.format(designTime);
}
/**
*
* 获取前几天的日期
*/
public static String getPrefixDate(String count){
Calendar cal = Calendar.getInstance();
int day = 0-Integer.parseInt(count);
cal.add(Calendar.DATE,day); // int amount 代表天数
Date datNew = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
return format.format(datNew);
}
/**
* 日期转换成字符串
* @param date
* @return
*/
public static String dateToString(Date date){
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
return format.format(date);
}
/**
* 字符串转换日期
* @param str
* @return
*/
public static Date stringToDate(String str){
//str = " 2008-07-10 19:20:00 " 格式
SimpleDateFormat format = new SimpleDateFormat(formatPattern);
if(!str.equals("")&&str!=null){
try {
return format.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
//java中怎样计算两个时间如:“21:57”和“08:20”相差的分钟数、小时数 java计算两个时间差小时 分钟 秒 .
public void timeSubtract(){
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date begin = null;
Date end = null;
try {
begin = dfs.parse("2004-01-02 11:30:24");
end = dfs.parse("2004-03-26 13:31:40");
} catch (ParseException e) {
e.printStackTrace();
}
long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
long day1 = between / (24 * 3600);
long hour1 = between % (24 * 3600) / 3600;
long minute1 = between % 3600 / 60;
long second1 = between % 60;
System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分"
+ second1 + "秒");
}
}
相关文章推荐
- eclipse导入Android工程项目后出现错误汇总
- 未知宽高元素水平垂直居中
- 纯js实现瀑布流布局及ajax动态新增数据
- 重建所有表索引
- HTML5之2D物理引擎 Box2D for javascript Games 系列 第二部分
- 配置Docker beta for Mac访问私有镜像仓库
- LTE-室分问题定位思路
- 什么是Angular? 我们为什么要学习它?
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- javascript 小知识(技巧、方法) ---面包屑导航 文字上下滚动
- 自定义Drawable Demo(圆角/圆形图片,Material动画)
- [转载]C#4.0中的可选参数和命名参数
- 排序——简单排序算法
- JVM内存杂记1
- iOS 9新功能
- Html层模型中relative和absolute组合使用
- 在Windows下部署物联网平台SiteWhere
- MySQL数据类型总结
- python字符串基本知识
- 行内元素水平间隔有关问题