Android-日付類の使用
本文の合集はすでに本を編纂して、高級Android開発は実戦を強化して、各位の読友の提案と指導を歓迎します.京東で購入可能:https://item.jd.com/12385680.html
Androidの日付類、DateとCalendar、Date表示、Calendar計算.処理順序:String->Date->Calendar.例では、(1)日付間隔日数、(1)日付間隔日数、(2)文字列変換Dateクラス;(3)取得前の日付;(4)今日を超えるか否かを判断する.
コード:
Androidの日付類、DateとCalendar、Date表示、Calendar計算.処理順序:String->Date->Calendar.例では、(1)日付間隔日数、(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.
* , .
*
* 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> 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 allSteps = new ArrayList<>();
ArrayList 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> totalData = new ArrayList<>();
for (int i = 0; i < allSteps.size(); ++i) {
Pair 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();
}
}