請自己寫一個簡易版的SimpleDateFormat
並利用Calendar 取得當前系統時間
Main.java- Calendar calender = Calendar.getInstance();
- SimpleDate simple = new SimpleDate("yyyy/MM/dd/ hh:mm:ss");
- System.out.println(simple.format(calender));
複製代碼 SimpleDate .java- package tw.kuas.edu.tw;
- import java.util.Calendar;
- public class SimpleDate {
- private String format;
- public SimpleDate(String format)
- {
- this.format = format;
- }
-
-
- public String format(Calendar c)
- {
- this.format = format.replaceAll("yyyy", String.valueOf(c.get(Calendar.YEAR)));
- this.format = format.replaceAll("MM", String.valueOf(c.get(Calendar.MONTH)+1));
- this.format = format.replaceAll("dd", String.valueOf(c.get(Calendar.DATE)));
- this.format = format.replaceAll("hh", String.valueOf(c.get(Calendar.HOUR)));
- this.format = format.replaceAll("mm", String.valueOf(c.get(Calendar.MINUTE)));
- this.format = format.replaceAll("ss", String.valueOf(c.get(Calendar.SECOND)));
-
- return format;
- }
- }
複製代碼 |