JAVA一些常用的时间操作
项目中经常有对时间进行处理的需求,下面是一些常用的操作整理,方便以后再次使用以及做相关复习。
1.字符串转换为日期
/**
* 字符串转换为日期
* @param dateStr 需要转换的日期
* @param dateFormat 日期格式yyyy-MM-dd/yyyy-MM-dd HH:mm:ss
*/
public static Date toDate(String dateStr, SimpleDateFormat dateFormat) throws ParseException{
Date date = null;
try {
date = dateFormat.parse(dateStr);
} catch (ParseException e) {
logger.debug("Fail to convert String to Date, {}", dateStr);
}
return date;
}
2.时间戳转日期
/**
* 时间戳转日期
* @param date
* @return
*/
public static String dateToTime(long time, SimpleDateFormat dateFormat) throws ParseException{
String data = null;
try {
dateFormat.format(new Date(time*1000));
} catch (Exception e) {
logger.debug("Fail to convert long to Date, {}", time);
}
return data;
}
3.日期格式化成字符串
/**
* 日期格式化成字符串
* @param date
* @param dateFormat
* @return
* @throws ParseException
*/
public static String toString(Date date, SimpleDateFormat dateFormat) throws ParseException{
return dateFormat.format(date);
}
4.获取指定日期之前或之后的日期 ,十分秒为00:00:00
/**
* 获取指定日期之前或之后的日期
* @param date
* @param num 正数为之后,负数为之前
* @return yyyy-MM-dd 00:00:00
*/
public static Date getSpecificDate(Date date, int num){
Calendar todayCal = Calendar.getInstance();
todayCal.setTime(date); Calendar c = Calendar.getInstance();
c.set(todayCal.get(Calendar.YEAR), todayCal.get(Calendar.MONTH), todayCal.get(Calendar.DAY_OF_MONTH) + num, 0, 0, 0);
return c.getTime();
}
5.获取指定日期之前或之后的日期 ,时分秒为当前的
/**
* 获取指定日期之前或之后的日期
* @param date
* @param num 正数为之前,负数为之后
* @return yyyy-MM-dd + 当前的时分秒
*/
public static Date getSpecificDateAndHhMmSs(Date date,int num){
Calendar c = Calendar.getInstance();
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day - num);
return c.getTime();
}
6.将time类型的时间字符串 转换成 时、分
/**
* 将time类型的时间字符串 转换成 时、分
* HH-mm-ss -->> HH-mm
* @param time
* @return
*/
public static String timeToHHMM(String time){
return time.substring(0, time.length() - 3);
}
7.获取某个日期的时、分
/**
* 获取某个日期的时、分
* @param date
* @return HH-mm
*/
public static String getHM(Date date){
Calendar ca = Calendar.getInstance();
ca.setTime(date);
Integer hour = ca.get(Calendar.HOUR_OF_DAY);//小时
Integer minute = ca.get(Calendar.MINUTE);//分
String rs_hour = hour.toString();
String rs_minute = minute.toString(); if (rs_hour.length() == 1){
rs_hour = "0" + hour;
}
if(rs_minute.length() == 1){
rs_minute = "0" + minute;
} return rs_hour + ":" + rs_minute;
}
8.time类型的时间字符串 -->> 零点开始的秒数
/**
* time类型的时间字符串 -->> 零点开始的秒数
* @param time HH-mm / HH-mm-ss
* @return
*/
public static Integer timeToSeconds(String time){
String[] timeSplit = null;
int hours = 0,minutes = 0,seconds = 0;
try {
timeSplit = time.split(":");
if (timeSplit.length == 2) {
hours = Integer.valueOf(timeSplit[0])*60*60;
minutes = Integer.valueOf(timeSplit[1])*60;
}else if(timeSplit.length == 3){
hours = Integer.valueOf(timeSplit[0])*60*60;
minutes = Integer.valueOf(timeSplit[1])*60;
seconds = Integer.valueOf(timeSplit[2]);
}else{
logger.debug("Fail to convert the time, {}", time);
}
} catch (Exception e) {
logger.debug("Fail to convert the time, {}", time);
throw e;
} return hours + minutes + seconds;
}
9.零点开始的秒数转时间 -->> HH-mm-ss
/**
* 零点开始的秒数转时间 -->> HH-mm-ss
* @param durationSeconds
* @return
*/
public static String getDuration(int durationSeconds){
int hours = durationSeconds /(60*60);
int leftSeconds = durationSeconds % (60*60);
int minutes = leftSeconds / 60;
int seconds = leftSeconds % 60; StringBuffer sBuffer = new StringBuffer();
sBuffer.append(addZeroPrefix(hours));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(minutes));
sBuffer.append(":");
sBuffer.append(addZeroPrefix(seconds)); return sBuffer.toString();
} public static String addZeroPrefix(int number){
if(number < 10)
return "0"+number;
else
return ""+number;
}
10.比较两个日期相差的秒数
/**
* 比较两个日期相差的秒数
* @param startDate
* @param endDate
* @return
*/
public static int getTimeSeconds(Date startDate,Date endDate) {
long a = endDate.getTime();
long b = startDate.getTime();
return (int)((a - b) / 1000);
}
11.判断两个时间段是否存在交集
/**
* 比较两个日期相差的秒数
* @param startDate
* @param endDate
* @return
*/
public static int getTimeSeconds(Date startDate,Date endDate) {
long a = endDate.getTime();
long b = startDate.getTime();
return (int)((a - b) / 1000);
}
12.获取指定日期是星期几(1-7分别代表周一至周日)
/**
* 获取指定日期是星期几(1-7分别代表周一至周日)
* @return
*/
public static int DayOfWeek(Date date){
Calendar now = Calendar.getInstance();
now.setTime(date);
boolean isFirstDay = (now.getFirstDayOfWeek() == Calendar.SUNDAY);
int weekday = now.get(Calendar.DAY_OF_WEEK);
if(isFirstDay){
weekday = weekday - 1;
if(weekday == 0){
weekday = 7;
}
}
return weekday;
}
JAVA一些常用的时间操作的更多相关文章
- java中常用的时间操作
最近项目设计时间的转换和计算,长时间没用时间操作了,感觉手有点生,所以在这里记录一下: Date 常用的方法: getTime() .setTime(): SimpleDateFormate 常用的方 ...
- Java Calendar 类的时间操作
Java Calendar 类的时间操作 标签: javaCalendar时间Date 2013-07-30 17:53 140401人阅读 评论(7) 收藏 举报 分类: 所有(165) Java ...
- python中常用的时间操作
python中常用的时间模块有time和datetime,以下是这两个模块中常用的方法: #先引入模块 import timefrom datetime import datetiem, timezo ...
- Java Calendar 类的时间操作.RP
JavaCalendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单. 演示了获取时间,日期时间的累加和累减,以及比较. 原文地址:blog.csdn.NET/joyous/a ...
- Java【常用的日期操作】
目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...
- 深入理解Java常用类-----时间日期
除了String这个类在日常的项目中比较常用之外,有关时间和日期的操作也是经常遇到的,本篇就讲详细介绍下Java API中对时间和日期的支持.其实在Java 8之前时间日期的API并不是很好用,以至于 ...
- Java日期时间操作基础——包含JDK1.8时间操作新特性
JDK1.7日期时间操作 示例小结 public class DateTest { public static final String FORMAT_DATE = "yyyy-MM-dd& ...
- paip.日期时间操作以及时间戳uapi php java python 总结
paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...
- JAVA中的时间操作
java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...
随机推荐
- iOS开发——多线程篇——NSThread
一.基本使用1.创建和启动线程一个NSThread对象就代表一条线程 创建.启动线程NSThread *thread = [[NSThread alloc] initWithTarget:self s ...
- Two Strings Are Anagrams
Write a method anagram(s,t) to decide if two strings are anagrams or not. 判断两个字符串里的字符是否相同,也就是是否能够通过改 ...
- C#高级编程笔记 Day 2, 2016年8月 31日 构造函数
1.构造函数: 实例构造函数(只要创建了对象,就会执行)一般使用 this 关键字区分成员字段和同名的参数.可以把构造函数定义为private 或 protected .这样不相关的类也不能访问他们. ...
- Open CV缩放图像
缩放图像是图像处理中需要经常使用的操作.太小的图像在图像识别中不能很好的处理,需要将其放大,太大的图像不方便储存,需要将其缩小,下面记录OpenCV图片缩放方法. 缩放函数 , , int inter ...
- huffman编码压缩算法(转)
参考:http://blog.csdn.net/sunmenggmail/article/details/7598012 笔试时遇到的一道题.
- Webclent基本操作
/** * @Title: webclientTest.java * @Package webclient * @Description: TODO(用一句话描述该文件做什么) * @author A ...
- c++写入txt文件
简单方式: #include "stdafx.h" #include <iostream> #include <iomanip> #include < ...
- Android常用控件
Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...
- PHP无法编译undefined reference to `libiconv_open
./configure --prefix=/usr/local/php52 make时提示:.....................................................e ...
- 解读Unity中的CG编写Shader系列四(unity中的圆角矩形shader)
转自 http://www.itnose.net/detail/6097625.html 上篇文章中我们掌握了表面剔除和剪裁模式 这篇文章将利用这些知识实现一个简单的,但是又很常用的例子:把一张图片做 ...