我正在尝试确定一整天都经过了多少小时,分钟,秒。
计时器在每个新的一天午夜重新启动。
因此,如果当前时间是下午2:15,我需要将其计算为14小时15分钟。
我知道如何在.Net中轻松完成此操作,但不幸的是我对Android和Java的日历/日期类不熟悉。
此时似乎有点弃用了Date类。
有人能指出我正确的方向吗?
感谢。
答案 0 :(得分:3)
您可以执行以下操作:
// get a calendar instance for midnight time
Calendar midnightCalendar = Calendar.getInstance();
midnightCalendar.set(Calendar.HOUR_OF_DAY, 0);
midnightCalendar.set(Calendar.MINUTE, 0);
midnightCalendar.set(Calendar.SECOND, 0);
long midnightTimeInMillis = midnightCalendar.getTimeInMillis();
// get a calendar instance for current time
Calendar currentTimeCalendar = Calendar.getInstance();
long currentTimeInMillis = currentTimeCalendar.getTimeInMillis();
// get the difference
long diffInMillis = currentTimeInMillis - midnightTimeInMillis;
// get the hours
int hours = (int)(diffInMillis / 3600 * 1000);
// get the minutes
int minutes = (int)(diffInMillis % 3600000) / (60 * 1000);
答案 1 :(得分:2)
您可以使用Calendar
类来获取此信息。您可以将Calendar
设置为午夜,如下所示:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
然后你可以用millis来获得时间或使用其他方法来获得小时/分钟/秒等。
cal.getTimeInMillis();
答案 2 :(得分:1)
package com.umeng.comm.utils;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author mrsimple
*/
public class TimeUtils {
public static final long ONE_SECOND = 1000L;
public static final long ONE_MINUTE = 60000L;
public static final long ONE_HOUR = 3600000L;
public static final long ONE_DAY = 86400000L;
// private static final long ONE_WEEK = 604800000L;
/**
*
*/
public static final int ONE_YEAR_DAYS = 365;
/**
*
*/
public static final String JUST_NOW = "just now";
/**
*
*/
public static final String MINUTE_AGO = "munites ago";
/**
*
*/
public static final String HOUR_AGO = "hours ago ";
// private static final String TODAY = "Today ";
public static final String YESTERDAY = "Yestoday ";
public static final String THE_DAY_BEFORE_YESTERDAY = "the day before_yestoday ";
/**
*
*/
public static final String YEAR_AGO = "years ago";
/**
*
*/
@SuppressLint("SimpleDateFormat")
private static SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
*
* @param timeStr
* @return
*/
public static String format(String timeStr) {
if (!TextUtils.isEmpty(timeStr)) {
try {
Date date = mFormat.parse(timeStr);
return format(date);
} catch (Exception e) {
e.printStackTrace();
}
}
return timeStr;
}
/**
*
* @param timeStr
* @return
*/
public static long getTime(String timeStr){
if ( TextUtils.isEmpty(timeStr) ) {
return 0;
}
try {
return mFormat.parse(timeStr).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
*
* @param date
* @return
*/
public static String format(Date date) {
int days = getDayWithToday(date);
long timeGap = getTimeGap(date);
if (days == 0) {
// return TODAY + getSimpleTimeStr(date);
return getTodaysDateString(timeGap);
}
if (days == 1) {
return YESTERDAY + getSimpleTimeStr(date);
}
if (days == 2) {
return THE_DAY_BEFORE_YESTERDAY + getSimpleTimeStr(date);
}
return getSimpleDateStr(date);
}
private static long getTimeGap(Date date) {
final long now = System.currentTimeMillis();
return now - date.getTime();
}
private static String getTodaysDateString(long timegap) {
if (timegap < ONE_MINUTE) {
return JUST_NOW;
} else if (timegap <= ONE_HOUR) {
return timegap / ONE_MINUTE + MINUTE_AGO;
}
return timegap / ONE_HOUR + HOUR_AGO;
}
/**
* @param date
* @return
*/
private static int getDayWithToday(Date date) {
// today's calendar
Calendar calendar = Calendar.getInstance();
int today = calendar.get(Calendar.DAY_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
calendar.setTime(date);
int createYear = calendar.get(Calendar.YEAR);
int feedCreateDay = calendar.get(Calendar.DAY_OF_YEAR);
return (today - feedCreateDay) + (year - createYear) * ONE_YEAR_DAYS;
}
/**
* @return
*/
private static String getSimpleTimeStr(Date date) {
SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();
sdf.applyPattern("HH:mm");
return sdf.format(date);
}
/**
* @param date
* @return
*/
private static String getSimpleDateStr(Date date) {
SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();
int days = getDayWithToday(date);
String prefix = "";
if (days >= ONE_YEAR_DAYS) {
sdf.applyPattern("MM-dd");
prefix = days / ONE_YEAR_DAYS + YEAR_AGO;
} else {
sdf.applyPattern("MM-dd HH:mm");
}
return prefix + sdf.format(date);
}
}
用法:
TimeUtils.format(&#34; 2014-12-19 10:24:01&#34;);