2017-10-04 56 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小时格式它也会进入程序