Android
には、今月を含む過去12ヶ月間の折れ線グラフのラベルを生成できます。Swift 3 - カレンダークラスを使用して過去12ヶ月を取得することに相当するAndroid
私TimeDateHelper
クラスでは:私の他のLineChartActivity
クラスで
public static DateFormat getMonthYearChartLabelFormat() {
DateFormat monthYearChartLabelFormat = new SimpleDateFormat("MMM ''yy", Locale.US);
monthYearChartLabelFormat.setTimeZone(TimeZone.getDefault());
return monthYearChartLabelFormat;
}
:
private void setUpMonthLabels() {
this.labels = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.add(Calendar.MONTH, -11);
for (int i = 0; i < 12; i++) {
this.labels.add(TimeDateHelper.getMonthYearChartLabelFormat().format(cal.getTime()));
cal.add(Calendar.MONTH, 1);
}
}
Swift 3
のCalendar
クラスのenumerateDates
方法があまりにも混乱しています。私はそれを使うかどうか分からない。私が望むのは今月を含め過去12ヶ月間です。たとえば、今月は12月ですので、出力は次のようになります。
Jan '16
Feb '16
Mar '16
Apr '16
May '16
Jun '16
Jul '16
Aug '16
Sep '16
Oct '16
Nov '16
Dec '16
これをSwift 3でどうやって行うことができますか?
**** **** PROGRESS
はこれまでのところ、私はいくつかの書式が設定されている:
let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
return formatter
}()
func getMonthYearChartLabelString(fromDate date: Date) -> String {
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMM ''yy", options: 0, locale: Locale(identifier: "en-US"))
return dateFormatter.string(from: date)
}
アポストロフィは元(カントー何らかの理由で動作しません、それは12月を示しています。 12月16日が表示されるべきときは16)。
に必要な値を取得しますあなたは、文字列または日付オブジェクトとして出力をしたいですか? – Alexander
文字列、今まで入れた書式で投稿を編集します – Rafi