Android - 日期类的使用
jopen
9年前
Android的日期类, Date和Calendar, Date显示, Calendar计算.
处理顺序: String -> Date -> Calendar.
示例实现了几个比较常见的方法:
(1) 日期相隔天数;
(2) 字符串转换Date类;
(3) 获取之前的日期;
(4) 判断是否超过今天等.
代码:
package me.chunyu.Pedometer.manager; import android.util.Pair; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Locale; import me.chunyu.Pedometer.database.counter.DailyData; /** * 日期管理器, 输入日期的格式"yyyy-MM-dd"的字符串数组, 可能不连续. * 提取步数, 需要把拥有步数的项提取出来, 未有步数的项至为0. * 使用时, 再根据日期进行统计. * <p/> * Created by wangchenlong on 15/8/17. */ public class DateManager { private static DateManager sInstance; // 单例 private final RecordManager mRecordManager; // 数据管理器 private final SimpleDateFormat sFormatter; // 格式化 private DateManager() { mRecordManager = RecordManager.getInstance(); sFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); } public static DateManager getInstance() { if (sInstance == null) { sInstance = new DateManager(); } return sInstance; } /** * 从RecordManager获取数据, 转换为步数数组, 从当前日期一直至最早日期. * * @return 步数数组[当前日期, 至, 最早日期] */ public ArrayList<Pair<String, Integer>> getDataDates() { String[] days = mRecordManager.getSortedFilenames(); if (days.length <= 0) { return null; } String firstDay = days[0]; String lastDay = days[days.length - 1]; if (isAfterToday(lastDay)) { lastDay = sFormatter.format(new Date()); } long n = daysBetweenTwoDates(firstDay, lastDay) + 1; ArrayList<Integer> allSteps = new ArrayList<>(); ArrayList<String> allDays = new ArrayList<>(); for (int i = 0; i < n; ++i) { String perDay = getBeforeDay(lastDay, i); DailyData dailyData = mRecordManager.getDailySteps(perDay); if (dailyData != null) { int[] steps = dailyData.steps; int maxStep = steps[steps.length - 1]; allSteps.add(maxStep); } else { allSteps.add(0); } allDays.add(perDay); } ArrayList<Pair<String, Integer>> totalData = new ArrayList<>(); for (int i = 0; i < allSteps.size(); ++i) { Pair<String, Integer> dateWithStep = Pair.create(allDays.get(i), allSteps.get(i)); totalData.add(dateWithStep); } return totalData; } /** * 判断两个日期相隔的天数 * * @param earlyDay 较早的日期 * @param lateDay 较晚的日期 * @return 相隔天数 */ public long daysBetweenTwoDates(String earlyDay, String lateDay) { Date earlyDate = getDateFromStr(earlyDay); Date lateDate = getDateFromStr(lateDay); Calendar startCalendar = Calendar.getInstance(); Calendar lateCalendar = Calendar.getInstance(); startCalendar.setTime(earlyDate); lateCalendar.setTime(lateDate); long diff = lateCalendar.getTimeInMillis() - startCalendar.getTimeInMillis(); return diff / (24 * 60 * 60 * 1000); } /** * 把日期字符串转换为Date类 * * @param day 日期字符串 * @return 日期Data类 */ public Date getDateFromStr(String day) { Date date; try { date = sFormatter.parse(day); } catch (ParseException e) { e.printStackTrace(); return null; } return date; } /** * 获取之前某天的日期 * * @param day 时间基准 * @param i 相差天数 * @return 前几天的日期 */ public String getBeforeDay(String day, int i) { Date date = getDateFromStr(day); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, i * (-1)); return sFormatter.format(calendar.getTime()); } /** * 判断时间是否超过今日 * * @param day 时间 * @return 是否 */ public boolean isAfterToday(String day) { Date lateDate = getDateFromStr(day); Date todayDate = new Date(); return lateDate.getTime() > todayDate.getTime(); } }
来自: http://blog.csdn.net//caroline_wendy/article/details/47751827