回答編集履歴

1

回答追記

2020/02/01 15:16

投稿

TakeOne
TakeOne

スコア6299

test CHANGED
@@ -53,3 +53,83 @@
53
53
  localeは`japanese (current)`のようになります。
54
54
 
55
55
  インスタンスの生成の仕方の違いで中に設定されているlocaleが変わるだけです。
56
+
57
+
58
+
59
+ ---
60
+
61
+ (2/2 0:15 追記)
62
+
63
+
64
+
65
+ すみません。質問の意図を勘違いしていました。
66
+
67
+ DateFormatterのlocaleプロパティと別のCalendarインスタンスのlocaleプロパティは連動しているのか?
68
+
69
+ と聞いているのではなく、
70
+
71
+ DateFormatterのlocaleプロパティと同じDateFormatterインスタンスのcalendarプロパティは連動しているのか?
72
+
73
+ と聞いているのですね。
74
+
75
+
76
+
77
+ ```swift
78
+
79
+ let dateString = "2000/8/24"
80
+
81
+ let formatter = DateFormatter()
82
+
83
+ formatter.dateFormat = "yyyy/MM/dd"
84
+
85
+ print(formatter.locale!)
86
+
87
+ print(formatter.calendar!)
88
+
89
+ print(formatter.calendar.locale!)
90
+
91
+ print("date: (formatter.date(from: dateString)!)")
92
+
93
+
94
+
95
+ formatter.locale = Locale(identifier: "ja_JP")
96
+
97
+ print(formatter.locale!)
98
+
99
+ print(formatter.calendar!)
100
+
101
+ print(formatter.calendar.locale!)
102
+
103
+ print("date: (formatter.date(from: dateString)!)")
104
+
105
+ ```
106
+
107
+ iPhoneの設定を「和暦」にして、上記のコードを実行してみたところ、
108
+
109
+ 次の結果が得られましたので、localeを設定すると、そのlocaleに
110
+
111
+ 対応したCalendarインスタンスがcalendarプロパティに設定される
112
+
113
+ と考えて良いと思います。
114
+
115
+
116
+
117
+ ```
118
+
119
+ en@calendar=japanese (current)
120
+
121
+ japanese (current)
122
+
123
+ en@calendar=japanese (current)
124
+
125
+ date: 4018-08-23 15:00:00 +0000
126
+
127
+ ja_JP (fixed)
128
+
129
+ gregorian (fixed)
130
+
131
+ ja_JP (fixed)
132
+
133
+ date: 2000-08-23 15:00:00 +0000
134
+
135
+ ```