2012年9月18日火曜日

Java 時刻を表示

以下はサンプル


import java.util.Calendar;


public class checktime{

 public static void main(String[] args) {
  
  String Timedate = checkTime();
  System.out.println(Timedate);
  

 }

 private static String  checkTime(){
        
     String modori;
     Calendar cal1 = Calendar.getInstance();  

     int year = cal1.get(Calendar.YEAR);        
     int month = cal1.get(Calendar.MONTH) + 1;  
     int day = cal1.get(Calendar.DATE);         
     int hour = cal1.get(Calendar.HOUR_OF_DAY); 
     int minute = cal1.get(Calendar.MINUTE);    
     int second = cal1.get(Calendar.SECOND);    

     StringBuffer dow = new StringBuffer();
     switch (cal1.get(Calendar.DAY_OF_WEEK)) {  
       case Calendar.SUNDAY: dow.append("日曜日"); break;
       case Calendar.MONDAY: dow.append("月曜日"); break;
       case Calendar.TUESDAY: dow.append("火曜日"); break;
       case Calendar.WEDNESDAY: dow.append("水曜日"); break;
       case Calendar.THURSDAY: dow.append("木曜日"); break;
       case Calendar.FRIDAY: dow.append("金曜日"); break;
       case Calendar.SATURDAY: dow.append("土曜日"); break;
     }

     
     modori =String.format("%04d/%02d/%02d%s%02d:%02d:%02d"
       ,year,month,day,dow,hour,minute,second);

     return modori;
     
     
     }
}


0 件のコメント:

コメントを投稿