在编写代码中,会经经常使用到时间Date这个类,小编整理了一些经常使用的时间工具类。供大家參考。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone; /**
* 日期工具类
*/
public class DateUtils { /**
* 获取格林威治时间(1970年至今的秒数)
*/
public static long getGMTime1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/Greenwich"));
String format = sdf.format(new Date());
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date gmDate = null;
try {
gmDate = sdf1.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return gmDate.getTime() / 1000;
} /**
* 获取格林威治时间 即1970年至今的秒数
*/
public static long getGMTime2() {
int round = (int) (System.currentTimeMillis() / 1000);
return round;
} /**
* 获取时间HH:mm:ss
*
* @return
*/
public static String getCurrentTime() {
String time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
//"\\s"以空格截断
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
} /**
* 获取当前时间的年月日时分秒
* @return
*/
public static String current() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒";
} /**
* 得到昨天的日期
*
* @return
*/
public static String getYesterdayDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String yestoday = sdf.format(calendar.getTime());
return yestoday;
} /**
* 得到今天的日期
*
* @return
*/
public static String getTodayDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
return date;
} /**
* 得到明天的日期
*
* @return
*/
public static String getTomorrowDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tomorrow = sdf.format(calendar.getTime());
return tomorrow;
} /**
* 时间转化为时间格式
*
* @param timeStamp
* @return
*/
public static String timeStampToStr(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(timeStamp * 1000);
return date;
} /**
* 时间转化为时间格式
*
* @param timeStamp
* @return
*/
public static String timeStampToStr1(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date = sdf.format(timeStamp * 1000);
return date;
} /**
* 时间转化为时间(几点)
*
* @param time
* @return
*/
public static String timeStampToTime(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(time * 1000);
return date;
} /**
* 将日期格式转化为时间(秒数)
*
* @param time
* @return
*/
public static long getStringToDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
} /**
* 将日期格式转化为时间(秒数)
*
* @param time
* @return
*/
public static long getString2Date(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
} /**
* 推断是否大于当前时间
*
* @param time
* @return
*/
public static boolean judgeCurrTime(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
try {
date = sdf.parse(time);
long t = date.getTime();
long round = System.currentTimeMillis();
if (t - round > 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace(); }
return false;
} /**
* 推断是否大于当前时间
*
* @param time
* @return
*/
public static boolean judgeCurrTime(long time) {
long round = System.currentTimeMillis();
if (time - round > 0) {
return true;
} else {
return false;
}
} /**
* 比較后面的时间是否大于前面的时间
*
* @param
* @return
*/
public static boolean judgeTime2Time(String time1, String time2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
//转化为时间
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
//获取秒数作比較
long l1 = date1.getTime() / 1000;
long l2 = date2.getTime() / 1000;
if (l2 - l1 > 0) {
return true;
} else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
} /**
* 得到日期 yyyy-MM-dd
*
* @param time
* @return
*/
public static String formatDate(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(time * 1000);
return date;
} /**
* 得到时间 HH:mm:ss
*
* @param timeStamp
* @return
*/
public static String getTime(long timeStamp) {
String time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(timeStamp * 1000);
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
} /**
* 将一个时间转换成提示性时间字符串,(多少分钟)
*
* @param timeStamp
* @return
*/
public static String timeStampToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
return time / 60 + "";
} /**
* 获得当前时间差
*
* @param timeStamp
* @return
*/
public static int nowCurrentTime(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = timeStamp - curTime;
return (int) time;
} /**
* 获取当前的时 -->flag==true
* 获取当前的分 -->flag==false
*
* @return
*/
public static String nowCurrentPoint(boolean flag) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(System.currentTimeMillis());
String[] split = date.split(":");
String hour = null;
String minute = null;
if (flag) {
if (split.length > 1) {
hour = split[0];
return hour;
}
} else {
if (split.length > 1) {
minute = split[1];
return minute;
}
}
return null;
} /**
* 将标准时间格式HH:mm:ss化为当前的时间差值
*
* @param str
* @return
*/
public static String StandardFormatStr(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(str);
long timeStamp = d.getTime(); long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp / 1000;
return time / 60 + "";
} catch (ParseException e) {
e.printStackTrace();
}
return null;
} /**
* 将一个时间转换成提示性时间字符串,如刚刚,1秒前
*
* @param timeStamp
* @return
*/
public static String convertTimeToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp; if (time < 60 && time >= 0) {
return "刚刚";
} else if (time >= 60 && time < 3600) {
return time / 60 + "分钟前";
} else if (time >= 3600 && time < 3600 * 24) {
return time / 3600 + "小时前";
} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
return time / 3600 / 24 + "天前";
} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 + "个月前";
} else if (time >= 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 / 12 + "年前";
} else {
return "刚刚";
}
} /**
* 日期变量转成相应的星期字符串
*
* @param date
* @return
*/ public static final int WEEKDAYS = 7;
//星期字符数组
public static String[] WEEK = {"周日", "周一", "周二", "周三",
"周四", "周五", "周六"}; public static String DateToWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
if (dayIndex < 1 || dayIndex > WEEKDAYS) {
return null;
}
return WEEK[dayIndex - 1];
} }

最新文章

  1. CentOS系统配置记录
  2. Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力)
  3. WF4.0 工作流设计器 传入参数问题记录?
  4. sublime 3
  5. twisted 学习笔记二:创建一个简单TCP客户端
  6. 【JAVA、C++】LeetCode 009 Palindrome Number
  7. [机器学习之13]降维技术——主成分分析PCA
  8. 如何查看linux系统下的各种日志文件 linux 系统日志的分析大全
  9. python循环for,range,xrange;while
  10. 第6次结对作业--郑锦伟&amp;古维城
  11. [python]numpy.mean()用法
  12. Es6对象的扩展和Class类的基础知识笔记
  13. 洛谷P3375 [模板]KMP字符串匹配
  14. 【DL】梯度下降小结
  15. 【QT】QPixmap和QImage在QLabel显示一张图像
  16. 服务器返回的http状态码
  17. 笔记本光驱位置装SSD固态硬盘(亲自试验)
  18. spring-kafka手动提交offset
  19. 其它终端设备连接gmail账户提示密码错误解决方法
  20. 一款简单实用的jQuery图片画廊插件

热门文章

  1. win下写任务提交给集群
  2. Win7如何开启管理员账户
  3. HTML5中音频视频标签使用
  4. Java模式开发之责任链模式
  5. Android Exception18(Stuido debug .....)
  6. The user specified as a definer (&amp;#39;root&amp;#39;@&amp;#39;%&amp;#39;) does not exist
  7. (三)Oracle学习笔记—— sql语句
  8. C-printf/sprintf/snprintf中的类型转换详解
  9. 转:nolock的替代方案-提交读快照隔离[行版本控制]
  10. &amp;quot;《 Serial Drivers 》by Alessandro Rubini&amp;quot; 学习笔记