在编写代码中,会经经常使用到时间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];
} }

Android经常使用工具类DateUtils(二)的更多相关文章

  1. java 日期工具类DateUtils

      日期工具类DateUtils CreateTime--2017年5月27日08:48:00Author:Marydon DateUtils.java-对日期类的进一步封装 import java. ...

  2. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  3. Android 软件管理工具类Utils

    Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...

  4. 一个使用命令行编译Android项目的工具类

    一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...

  5. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  6. android下载简单工具类

    功能是实现下载文件,图片或MP3等,为了简单起见使用单线程,此代码为MarsAndroid教程的复制品,放在此处,留着参考. 首先是一个得到字节流随后保存到内存卡上的工具类: package com. ...

  7. Android常用的工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...

  8. android 开发 常用工具类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...

  9. Android 自定义Dialog工具类

    由于项目的需要,系统的弹出框已经不能满足我们的需求,我们需要各式各样的弹出框,这时就需要我们去自定义弹出框了. 新建布局文件 dialog_layout.xml,将下面内容复制进去 <?xml ...

随机推荐

  1. 【监控】使用 Grafana、collectd 和 InfluxDB 打造现代监控系统

    参考资料:Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编辑器:http://www.oschina.net/p/grafana 使用 Grafana.collectd 和 ...

  2. html DOM 的继承关系

    零散的知识聚合在一起,就会形成力量,就有了生命力. 如各种语言的开发框架, 都是右各个碎片化的功能聚合在一起,构成有机地整体,便有了强大的力量.will be powerful! 如: jquery ...

  3. 【Android 工具类】经常使用工具类(方法)大全

    收集经常使用的工具类或者方法: 1.获取手机分辨率 /** * 获取手机分辨率 */ public static String getDisplayMetrix(Context context) { ...

  4. C++11 Lambda表达式简单解析

    C++11 新增了非常多特性,lambda 表达式是当中之中的一个.假设你想了解的 C++11 完整特性, 建议去http://www.open-std.org/看看新标准! 非常多语言都提供了 la ...

  5. Oracle学习笔记(5)——查询

    基本查询语句 SELECT [DISTINCT] column_name1,...|* FROM table_name [WHERE conditions] 在SQL*PLUS中设置格式 更改显示字段 ...

  6. java之super关键字

    super关键字: 在java中使用super来引用基类的成分. 程序code: class FatherClass{ public int value; public void f(){ value ...

  7. Jenkins插件开发资料

    原文地址:http://www.ciandcd.com/?p=181 Jenkins plugin 开发: Document http://hudson-ci.org/docs/index.html ...

  8. centos/rhel下实现nginx自启动脚本实例

    1. 建立脚本文件nginxd [root@could]# vi /etc/init.d/nginxd 插入以下内容   #!/bin/bash # # chkconfig: - 85 15 # de ...

  9. hdu5305Friends dfs

    //给一个无向图 , 每条边能够是online边也能够是offline边,问 //有多少种方法使得每一个节点的online边和offline边一样多 #include<cstdio> #i ...

  10. Git实战(四)状态转换

    上次的Git实战(三)环境搭建博文.我们大致解说了一下git的环境安装,今天我们解说一下Git的状态转换. 学习版本号控制工具.对工具进行版本号控制之间的状态转换很重要.毕竟Git仅仅是一个工具.假设 ...