📄 monthdateformat.html
字号:
<FONT color="green">126</FONT> <a name="line.126"></a><FONT color="green">127</FONT> /**<a name="line.127"></a><FONT color="green">128</FONT> * Creates a new formatter.<a name="line.128"></a><FONT color="green">129</FONT> * <a name="line.129"></a><FONT color="green">130</FONT> * @param zone the time zone used to extract the month and year from dates<a name="line.130"></a><FONT color="green">131</FONT> * passed to this formatter (<code>null</code> not permitted).<a name="line.131"></a><FONT color="green">132</FONT> * @param locale the locale used to determine the month names <a name="line.132"></a><FONT color="green">133</FONT> * (<code>null</code> not permitted).<a name="line.133"></a><FONT color="green">134</FONT> * @param chars the maximum number of characters to use from the month <a name="line.134"></a><FONT color="green">135</FONT> * names, or zero to indicate that the entire month name <a name="line.135"></a><FONT color="green">136</FONT> * should be used.<a name="line.136"></a><FONT color="green">137</FONT> * @param showYearForJan a flag that controls whether or not the year is<a name="line.137"></a><FONT color="green">138</FONT> * appended to the symbol for the first month of<a name="line.138"></a><FONT color="green">139</FONT> * each year.<a name="line.139"></a><FONT color="green">140</FONT> * @param showYearForDec a flag that controls whether or not the year is<a name="line.140"></a><FONT color="green">141</FONT> * appended to the symbol for the last month of<a name="line.141"></a><FONT color="green">142</FONT> * each year.<a name="line.142"></a><FONT color="green">143</FONT> */<a name="line.143"></a><FONT color="green">144</FONT> public MonthDateFormat(TimeZone zone, Locale locale, int chars, <a name="line.144"></a><FONT color="green">145</FONT> boolean showYearForJan, boolean showYearForDec) {<a name="line.145"></a><FONT color="green">146</FONT> this(<a name="line.146"></a><FONT color="green">147</FONT> zone, locale, chars, new boolean[] {showYearForJan, false, false, <a name="line.147"></a><FONT color="green">148</FONT> false, false, false, false, false, false, false, false, false,<a name="line.148"></a><FONT color="green">149</FONT> showYearForDec}, new SimpleDateFormat("yy")<a name="line.149"></a><FONT color="green">150</FONT> ); <a name="line.150"></a><FONT color="green">151</FONT> }<a name="line.151"></a><FONT color="green">152</FONT> <a name="line.152"></a><FONT color="green">153</FONT> /**<a name="line.153"></a><FONT color="green">154</FONT> * Creates a new formatter.<a name="line.154"></a><FONT color="green">155</FONT> * <a name="line.155"></a><FONT color="green">156</FONT> * @param zone the time zone used to extract the month and year from dates<a name="line.156"></a><FONT color="green">157</FONT> * passed to this formatter (<code>null</code> not permitted).<a name="line.157"></a><FONT color="green">158</FONT> * @param locale the locale used to determine the month names <a name="line.158"></a><FONT color="green">159</FONT> * (<code>null</code> not permitted).<a name="line.159"></a><FONT color="green">160</FONT> * @param chars the maximum number of characters to use from the month <a name="line.160"></a><FONT color="green">161</FONT> * names, or zero to indicate that the entire month name <a name="line.161"></a><FONT color="green">162</FONT> * should be used.<a name="line.162"></a><FONT color="green">163</FONT> * @param showYear an array of flags that control whether or not the<a name="line.163"></a><FONT color="green">164</FONT> * year is displayed for a particular month.<a name="line.164"></a><FONT color="green">165</FONT> * @param yearFormatter the year formatter.<a name="line.165"></a><FONT color="green">166</FONT> */<a name="line.166"></a><FONT color="green">167</FONT> public MonthDateFormat(TimeZone zone, Locale locale, int chars, <a name="line.167"></a><FONT color="green">168</FONT> boolean[] showYear, DateFormat yearFormatter) {<a name="line.168"></a><FONT color="green">169</FONT> if (locale == null) {<a name="line.169"></a><FONT color="green">170</FONT> throw new IllegalArgumentException("Null 'locale' argument.");<a name="line.170"></a><FONT color="green">171</FONT> }<a name="line.171"></a><FONT color="green">172</FONT> DateFormatSymbols dfs = new DateFormatSymbols(locale);<a name="line.172"></a><FONT color="green">173</FONT> String[] monthsFromLocale = dfs.getMonths();<a name="line.173"></a><FONT color="green">174</FONT> this.months = new String[12];<a name="line.174"></a><FONT color="green">175</FONT> for (int i = 0; i < 12; i++) {<a name="line.175"></a><FONT color="green">176</FONT> if (chars > 0) {<a name="line.176"></a><FONT color="green">177</FONT> months[i] = monthsFromLocale[i].substring(<a name="line.177"></a><FONT color="green">178</FONT> 0, Math.min(chars, monthsFromLocale[i].length())<a name="line.178"></a><FONT color="green">179</FONT> );<a name="line.179"></a><FONT color="green">180</FONT> }<a name="line.180"></a><FONT color="green">181</FONT> else {<a name="line.181"></a><FONT color="green">182</FONT> months[i] = monthsFromLocale[i];<a name="line.182"></a><FONT color="green">183</FONT> }<a name="line.183"></a><FONT color="green">184</FONT> }<a name="line.184"></a><FONT color="green">185</FONT> this.calendar = new GregorianCalendar(zone);<a name="line.185"></a><FONT color="green">186</FONT> this.showYear = showYear;<a name="line.186"></a><FONT color="green">187</FONT> this.yearFormatter = yearFormatter; <a name="line.187"></a><FONT color="green">188</FONT> <a name="line.188"></a><FONT color="green">189</FONT> // the following is never used, but it seems that DateFormat requires<a name="line.189"></a><FONT color="green">190</FONT> // it to be non-null. It isn't well covered in the spec, refer to <a name="line.190"></a><FONT color="green">191</FONT> // bug parade 5061189 for more info.<a name="line.191"></a><FONT color="green">192</FONT> this.numberFormat = NumberFormat.getNumberInstance();<a name="line.192"></a><FONT color="green">193</FONT> }<a name="line.193"></a><FONT color="green">194</FONT> <a name="line.194"></a><FONT color="green">195</FONT> /**<a name="line.195"></a><FONT color="green">196</FONT> * Formats the given date.<a name="line.196"></a><FONT color="green">197</FONT> * <a name="line.197"></a><FONT color="green">198</FONT> * @param date the date.<a name="line.198"></a><FONT color="green">199</FONT> * @param toAppendTo the string buffer.<a name="line.199"></a><FONT color="green">200</FONT> * @param fieldPosition the field position.<a name="line.200"></a><FONT color="green">201</FONT> * <a name="line.201"></a><FONT color="green">202</FONT> * @return The formatted date.<a name="line.202"></a><FONT color="green">203</FONT> */<a name="line.203"></a><FONT color="green">204</FONT> public StringBuffer format(Date date, StringBuffer toAppendTo,<a name="line.204"></a><FONT color="green">205</FONT> FieldPosition fieldPosition) {<a name="line.205"></a><FONT color="green">206</FONT> this.calendar.setTime(date);<a name="line.206"></a><FONT color="green">207</FONT> int month = this.calendar.get(Calendar.MONTH);<a name="line.207"></a><FONT color="green">208</FONT> toAppendTo.append(this.months[month]);<a name="line.208"></a><FONT color="green">209</FONT> if (this.showYear[month]) {<a name="line.209"></a><FONT color="green">210</FONT> toAppendTo.append(this.yearFormatter.format(date));<a name="line.210"></a><FONT color="green">211</FONT> }<a name="line.211"></a><FONT color="green">212</FONT> return toAppendTo; <a name="line.212"></a><FONT color="green">213</FONT> }<a name="line.213"></a><FONT color="green">214</FONT> <a name="line.214"></a><FONT color="green">215</FONT> /**<a name="line.215"></a><FONT color="green">216</FONT> * Parses the given string (not implemented).<a name="line.216"></a><FONT color="green">217</FONT> * <a name="line.217"></a><FONT color="green">218</FONT> * @param source the date string.<a name="line.218"></a><FONT color="green">219</FONT> * @param pos the parse position.<a name="line.219"></a><FONT color="green">220</FONT> * <a name="line.220"></a><FONT color="green">221</FONT> * @return <code>null</code>, as this method has not been implemented.<a name="line.221"></a><FONT color="green">222</FONT> */<a name="line.222"></a><FONT color="green">223</FONT> public Date parse(String source, ParsePosition pos) {<a name="line.223"></a><FONT color="green">224</FONT> return null; <a name="line.224"></a><FONT color="green">225</FONT> }<a name="line.225"></a><FONT color="green">226</FONT> <a name="line.226"></a><FONT color="green">227</FONT> /**<a name="line.227"></a><FONT color="green">228</FONT> * Tests this formatter for equality with an arbitrary object.<a name="line.228"></a><FONT color="green">229</FONT> * <a name="line.229"></a><FONT color="green">230</FONT> * @param obj the object.<a name="line.230"></a><FONT color="green">231</FONT> * <a name="line.231"></a><FONT color="green">232</FONT> * @return A boolean.<a name="line.232"></a><FONT color="green">233</FONT> */<a name="line.233"></a><FONT color="green">234</FONT> public boolean equals(Object obj) {<a name="line.234"></a><FONT color="green">235</FONT> if (obj == this) {<a name="line.235"></a><FONT color="green">236</FONT> return true;<a name="line.236"></a><FONT color="green">237</FONT> }<a name="line.237"></a><FONT color="green">238</FONT> if (!(obj instanceof MonthDateFormat)) {<a name="line.238"></a><FONT color="green">239</FONT> return false;<a name="line.239"></a><FONT color="green">240</FONT> }<a name="line.240"></a><FONT color="green">241</FONT> if (!super.equals(obj)) {<a name="line.241"></a><FONT color="green">242</FONT> return false;<a name="line.242"></a><FONT color="green">243</FONT> }<a name="line.243"></a><FONT color="green">244</FONT> MonthDateFormat that = (MonthDateFormat) obj;<a name="line.244"></a><FONT color="green">245</FONT> if (!Arrays.equals(this.months, that.months)) {<a name="line.245"></a><FONT color="green">246</FONT> return false;<a name="line.246"></a><FONT color="green">247</FONT> }<a name="line.247"></a><FONT color="green">248</FONT> if (!Arrays.equals(this.showYear, that.showYear)) {<a name="line.248"></a><FONT color="green">249</FONT> return false;<a name="line.249"></a><FONT color="green">250</FONT> }<a name="line.250"></a><FONT color="green">251</FONT> if (!this.yearFormatter.equals(that.yearFormatter)) {<a name="line.251"></a><FONT color="green">252</FONT> return false;<a name="line.252"></a><FONT color="green">253</FONT> }<a name="line.253"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -