2017-10-04 57 views
-1

我不明白java是如何工作的SimpleDateFormat。我想通過Locale顯示日期和時間。Android的日期格式

例如

Locale.setDefault(Locale.FRANCE); 
DateFormat correctDateFormatter =SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT, Locale.getDefault()); 
correctDateFormatter.format(date); 

所以我迫不及待地輸出24-H格式一樣

23:00

,但我得到它像

11 PM

我如何顯示時間和日期取決於本地和不使用時間模式(「hh:mm」等)?

+2

https://stackoverflow.com/a/18734471/8089931 –

+0

請參閱我的答案以顯示基於時間的區域設置 – Pehlaj

+0

請再次看看我的問題 – Lavan

回答

1

這將返回您在時區指定的格式,你需要:

String timeString = SimpleDateFormat("HH:mm", Locale.FRANCE).format(date); 
+0

我想使用SimpleDateFormat.getTimeInstance與樣式 – Lavan

+0

在這種情況下,我不認爲你想要的是可能的。樣式似乎是預先定義的。請參閱:https://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html –

+0

好的,我如何設置正確的時間模式取決於本地?我想顯示上午:下午在美國和沒有在歐洲 – Lavan

1
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.FRANCE); 

^h使用24小時,^h12小時格式。

1

試用時間String formattedLocalTime = getFormattedLocalTimeFromUtc("2016-02-16T04:46:28Z", "MM/dd/yyyy");

final String LOCAL_TIME_FORMAT = "EEE MMM dd HH:mm:ss z yyyy"; //This is your local date format 
final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; //This is input date format 
final String TIMEZONE_UTC = "UTC"; 

public static String getFormattedLocalTimeFromUtc(String utcTimeStamp, String outputFormat) { 

    String formattedTime = null; 
    if(!TextUtils.isEmpty(utcTimeStamp)) { 

      String localTime = null; 
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault()); 
      sdf.setTimeZone(TimeZone.getTimeZone(TIMEZONE_UTC)); 
      try { 
       localTime = sdf.parse(utcTimeStamp).toString(); 
      } 
      catch(ParseException e) { 
       e.printStackTrace(); 
      } 

      DateFormat inputDateFormat = new SimpleDateFormat(LOCAL_TIME_FORMAT); 
      inputDateFormat.setTimeZone(TimeZone.getTimeZone(TIMEZONE_UTC)); 
      Date date = null; 
      try { 
       date = inputDateFormat.parse(localTime); 
      } 
      catch(ParseException e) { 
       e.printStackTrace(); 
      } 

      DateFormat outputDateFormat = new SimpleDateFormat(outputFormat); 
      formattedTime = outputDateFormat.format(date); 
     } 
    } 
    return formattedTime; 
} 

HH.mm格式

H:一天中的小時(0-23) H:小時的AM/PM(0-11)

+0

請再次看我的問題 – Lavan

+0

你想顯示時間根據你的區域設置,對吧? – Pehlaj

+0

是根據設備本地 – Lavan

1

檢查SDF文檔here

哪裏有各種類型的,你可以用它來格式化你的日期字符的,

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.FRANCE); 

一旦你有這樣的:

simpledateFormat.format(date); 
2
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 

public class CheckDate { 

    public static void main(String[] args) { 

     Date date = new Date(); 
     Locale.setDefault(Locale.FRANCE); 
     SimpleDateFormat correctDateFormatter = new SimpleDateFormat("HH:mm", Locale.FRANCE); 
     correctDateFormatter.format(date); 
     System.out.println("24 hrs format :: "+correctDateFormatter.format(date)); 
    } 

} 

否則,如果你改變你的系統日期爲24小時格式它也會進入程序