ã¯ããã«
ãæçµåã®æ¬ç¨¿ã¯ãåºæ¬çãªJava APIã§ãããã¾ã§ã¨ãããã¦ããªãã£ãæ¥ä»ãæéãæä½ããAPIãç´¹ä»ãã¾ãã
ããªãAndroid SDKã«å«ã¾ããåºæ¬APIã¯ãç¾è¡ã®Java SE 6ã¨ã¾ã£ããåãã¨ããããã§ã¯ããã¾ãããAPI Levelãä½ãã¨ãJava SE 6ã«å«ã¾ããã¯ã©ã¹ãã¡ã½ããã®ä¸é¨ãå®è£ ããã¦ããªãå ´åãããã¾ãã
ãAPI Levelã®å¾®å¦ãªéãã¯ãAndroid Developersãµã¤ãã§ç¢ºèªããããã«ãã¦ãã ããã
対象èªè
ãAndroidã¢ããªã±ã¼ã·ã§ã³ã®éçºãå§ãããæ¹ã§ãJavaã¨Eclipseã®ããåºæ¬çãªç¥èãããæ¹ã対象ã¨ãã¾ãã
æ¥ä»ãæéå¦çã®åºæ¬
ãJavaã§æ¥ä»ãæå»ããã¤ããããã®åºæ¬ã¨ãªãã¯ã©ã¹ã¯ãjava.util.Dateã¨java.util.Calendarã¯ã©ã¹ã§ãã
ãjava.util.Dateã¯ã©ã¹ã¯ãç¹å®ã®æ¥ä»ã¨æå»ãä¿æããããã®ã¯ã©ã¹ã§ããä¿æããå¤ã¯ãã¨ããã¯ã¨å¼ã°ããã°ãªããã¸æ¨æºæã®1970å¹´1æ1æ¥00:00:00ããèµ·ç®ããããªç§ã¨ãªã£ã¦ãã¾ãã
ããã ç¾è¡ã®Javaã§ã¯ããã®ã¯ã©ã¹ã®ã¡ã½ããã使ããã¨ã¯ãã¾ãããã¾ãããã»ã¨ãã©ã®ã¡ã½ãããéæ¨å¥¨ã¨ãªã£ã¦ãããé常ã¯å½éåã«å¯¾å¿ããjava.util.Calendarã¯ã©ã¹ãæ¥ä»è¡¨ç¤ºç¨ã®ã¯ã©ã¹ãå©ç¨ãã¾ãã
ãCalendarã¯ã©ã¹ã¯ããã¾ãã¾ãªè¨èªãå°åã®æ¥ä»ãæå»ã®å¤ææ©è½ã«å¯¾å¿ããæ½è±¡ã¯ã©ã¹ã§ããç°ãªãæ¦ã«å¯¾ãã¦ããå ±éã®APIãæä¾ããã¦ãã¾ãã
java.util.Calendarã¯ã©ã¹ã®ä½¿ãæ¹
ãCalendarãªãã¸ã§ã¯ãã¯æ½è±¡ã¯ã©ã¹ãªã®ã§ãã¤ã³ã¹ã¿ã³ã¹ã®çæã¯ã³ã³ã¹ãã©ã¯ã¿ã使ç¨ããã«ãgetInstanceã¡ã½ããã使ç¨ãã¾ããgetInstanceã¡ã½ããã¯ãæå®ããããã±ã¼ã«ã¾ãã¯ã¿ã¤ã ã¾ã¼ã³ã«å¯¾å¿ããCalendarãªãã¸ã§ã¯ããè¿ãã¾ãããã±ã¼ã«ã¨ã¯ãããå°åã®å½ãè¨èªã¨ãã£ãæ å ±ã®ãã¨ã§ãã
ãå¼æ°ããªãå ´åã¯ãç¾å¨ã®ã·ã¹ãã æå»ã示ãããã©ã«ãã®Calendarãªãã¸ã§ã¯ããè¿ããã¾ãããªãããã©ã«ãã®Calendarãªãã¸ã§ã¯ãã¯ãCalendarã®å ·è±¡ãµãã¯ã©ã¹ã§ããGregorianCalendarã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã¨ãªã£ã¦ãã¾ãããã®ã¯ã©ã¹ã¯ãä¸çã®ã»ã¨ãã©ã®å°åã§ä½¿ç¨ãããæ¨æºçãªã«ã¬ã³ãã§ãã
ãã§ã¯ãDateã¯ã©ã¹ã¨Calendarã¯ã©ã¹ã使ã£ã¦ãç¾å¨æ¥æã表示ãã¦ã¿ã¾ããããä»åããããã¹ããã¥ã¼ã使ã£ã¦ãæåã表示ãã¦ãã¾ãã
ãã½ã¼ã¹ãã¡ã¤ã«ã¯ã次ã®ããã«ãªã£ã¦ãã¾ãã
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/parent" > </LinearLayout>
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sample1(); } // æååã®1è¡è¡¨ç¤º public void printString(String s) { // ãã¼ã¹ã®ã¬ã¤ã¢ã¦ãåå¾ LinearLayout parent = (LinearLayout) findViewById(R.id.parent); TextView tv = new TextView(this); tv.setTypeface(Typeface.MONOSPACE); // çå¹ ãã©ã³ãã®æå® tv.setText(s); parent.addView(tv); // ãã¥ã¼ã®è¿½å } public void sample1() { // Dateã¯ã©ã¹ã«ããç¾å¨æ表示 Date date = new Date(); printString("Date: " + date.toString()); // ããã©ã«ãã®Calendarãªãã¸ã§ã¯ã Calendar cal = Calendar.getInstance(); // å®è£ ã¯ã©ã¹å表示 printString(cal.getClass().getName()); // Calendarã¯ã©ã¹ã«ããç¾å¨æ表示 String tmp = "Calendar: " + cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND); printString(tmp); }
ãDateã¯ã©ã¹ã§ã¯ãtoStringã¡ã½ããã§ãæ¥æãæååã«å¤æã§ãã¾ãã
ãCalendarã¯ã©ã¹ã§ã¯ãgetã¡ã½ããã«ããæ¥æã®åå¤ãåå¾ãã¦è¡¨ç¤ºãã¦ãã¾ããgetã¡ã½ããã®å¼æ°ã«ã¯ãæ¥æã®ãã£ã¼ã«ãã示ãå®æ°ãæå®ãã¾ãããã®å®æ°ã¯ãCalendarã¯ã©ã¹ã§å®ç¾©ããã¦ããã主ãªå®æ°ã¯ä»¥ä¸ã®ã¨ããã§ãã
å®æ°å | 説æ |
YEAR | å¹´ |
MONTH | æ |
DATEãDAY_OF_MONTH | æã®æ¥ |
HOUR_OF_DAY | 24æéå¶ã®æå» |
AM_PM | ååãåå¾ |
HOUR | 12æéå¶ã®æå» |
MINUTE | å |
SECOND | ç§ |
MILLISECOND | ããªç§ |
DAY_OF_WEEK | ææ¥ |