本文主要是介绍Android获取各式时间类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在项目中需要获取各式各样的时间,而且也会有很多地方会调用到这些方法,所以我将它们归集于一个文件中,方便之后的使用。
现在我们需要了解一些相对基础获取时间的方法。
1.获取当前日历对象:
Calendar calendar = Calendar.getInstance();
2.获取当前时区下日期时间对应的时间戳:
calendar.getTimeInMillis();
3.获取标准格林尼治时间下日期时间对应的时间戳:
long unixTime = calendar.getTimeInMillis();
unixTime - TimeZone.getDefault().getRawOffset();
4.获取当前日期对象:
Date date = new Date();
5.获取当前时区下日期时间对应的时间戳:
date.getTimeInMillis();
6.设置日期时间格式:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
7.获取当前时区下日期时间对应的时间戳:
format.format(date);
现在来实现具体的方法。
1.获取时间戳:
public static long getTime() {Calendar calendar = Calendar.getInstance();// 获取当前日历对象long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳return unixTime;}
2.获取标准的时间:
public static String getStandardTime() {SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));Date curDate = new Date(System.currentTimeMillis());// 获取当前时间return formatter.format(curDate);}
3.获取与现在时间的时间差(秒):
public static int getDurationSecond(String time) {int durationSecond = 0;SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date;try {date = df.parse(time);MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);} catch (Exception e) {MyLog.e("TimeUtils getDurationSecond error=" + e);}return durationSecond;}
4.获取时间差:
public static String getDuration(String one, String two) {String duration = "";SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);Date date1;Date date2;try {date1 = df.parse(one);date2 = df.parse(two);int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);if (l > 60) {int hr = l / 60;int min = l % 60;duration = hr + "小时" + min + "分钟";} else {duration = l + "分钟";}} catch (Exception e) {e.printStackTrace();}return duration;}
完整代码展示:
public class MyTimeUtils {//获取时间戳public static long getTime() {Calendar calendar = Calendar.getInstance();// 获取当前日历对象long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳return unixTime;}public static String getTimeString() {return Long.toString(new Date().getTime());}//获取标准时间public static String getStandardTime() {SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));Date curDate = new Date(System.currentTimeMillis());// 获取当前时间return formatter.format(curDate);}// 获取与现在时间的时间差(秒)public static int getDurationSecond(String time) {int durationSecond = 0;SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);Date date;try {date = df.parse(time);MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);} catch (Exception e) {MyLog.e("TimeUtils getDurationSecond error=" + e);}return durationSecond;}// 获取时间差public static String getDuration(String one, String two) {String duration = "";SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span><span style="font-family: SimHei;">);</span>Date date1;Date date2;try {date1 = df.parse(one);date2 = df.parse(two);int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);if (l > 60) {int hr = l / 60;int min = l % 60;duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span>;} else {duration = <span style="font-family: SimHei;">l + "分钟";</span>}} catch (Exception e) {e.printStackTrace();}return duration;}// 获取与当前时间差public static String getcurDuration(String one) {String duration = "";SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);Date date1;Date date2;try {date1 = df.parse(one);date2 = new Date();int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);if (l > 60) {int hr = l / 60;int min = l % 60;duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span><span style="font-family: SimHei;">;</span>} else {duration =<span style="font-family: SimHei;"> l + "分钟";</span>}} catch (Exception e) {e.printStackTrace();}return duration;}/*** @return格式化当前日期和时间为字符串*/public static String mCurrentTime() {SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);String currenttime = df.format(new Date());return currenttime;}public static String parseBangTime(long time) {MyLog.out("time==>" + time);String timeTemp = "";if (time < 60) {timeTemp = time + BaseApplication.getInstance().getString(R.string.seconds_before);} else if (time < (60 * 60)) {timeTemp = time / 60 + BaseApplication.getInstance().getString(R.string.minutes_before);} else if (time < (3600 * 24)) {timeTemp = time / 3600 + BaseApplication.getInstance().getString(R.string.hour_before);} else if (time < (60 * 60 * 24 * 30)) {timeTemp = time / (3600 * 24) + BaseApplication.getInstance().getString(R.string.today_before);} else {timeTemp = time / (3600 * 24 * 30) + BaseApplication.getInstance().getString(R.string.month_before);}return timeTemp;}public static String getTimeStamp() {SimpleDateFormat dateFormat = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_two));String timeStamp = dateFormat.format(new Date());MyLog.e("getTimeStamp=" + timeStamp);return timeStamp;}public static String getCurrentDate(){SimpleDateFormat df = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show));String currentDate = df.format(new Date());return currentDate;}
}
这篇关于Android获取各式时间类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!