2016-12-19 2 views
0

私は時間を表示するために以下の方法を使用しています。時と分が1桁で表示されます

public static String getTimeByTimezone(String timeZone,Calendar localTime){  

     String time =""; 
     Calendar indiaTime = new GregorianCalendar(TimeZone.getTimeZone(timeZone)); 
     indiaTime.setTimeInMillis(localTime.getTimeInMillis()); 
     int hour = indiaTime.get(Calendar.HOUR); 
     int minute = indiaTime.get(Calendar.MINUTE); 
     int second = indiaTime.get(Calendar.SECOND); 
     int year = indiaTime.get(Calendar.YEAR); 
//  /int am = indiaTime.get(Calendar.AM_PM); 
     //System.out.printf("India time: %02d:%02d:%02d %02d\n", hour, minute, second, year); 

     time = hour+":"+minute; 

     if(indiaTime.get(Calendar.AM_PM)==0) 
      time=time+" AM"; 
     else 
      time=time+" PM"; 
return time; 
} 

だから、今まで来ている結果、何時間がある場合は少ないし、10のその一桁で表示される以下10分、その後であれば、一桁で登場し、数分と同じ。 例:時間 - >取得:9:15 AM予定:09:15 AM 分 - >取得:9:8 AM予定:09:08 AM 数字の処理方法を教えてもらえますか?

+1

おそらくあなたは[ 'DateFormat']を使用する必要があります(http:/を/docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html)。 – khelwood

+0

'08:00'は' String'、9:8は数字です(あるいは '08 + 09'と書いていますか?)。時間の 'String'表現のために' Formater'とあなたがしたい適切なフォーマットを使用してください – SomeJavaGuy

答えて

1

あなたはStringもっと自分の時間をフォーマットすることができます:

time = String.format("%02d:%02d", 1, 5); // = 01:05 

進むべき道をDateFormat使用することですが:

SimpleDateFormat f = new SimpleDateFormat("hh:mm a"); 
f.format(indiaTime.getTime()); // = 01:05 PM 
+0

それは私のためにうまくいった:) – Akash

関連する問題