2017-06-14 8 views
1

显示像设备日期时间格式的日期我有一个日期:如何在Android的

var date = new DateTime(2017,6,14,13,0,0); 

和一个TextView来显示它。 我想显示为“14/06/2017 13:00”时我设置设备的日期和时间为“使用24小时格式” 和“16/06/2017 1: 00 PM'当我取消''使用24小时格式'。

我该怎么办?

谢谢!

回答

0

在Xamarin.Android它比Android上有点不同,因为在穆罕默德的答案描述:

var date = new DateTime(2017, 6, 14, 13, 0, 0); 
var is24hour = Android.Text.Format.DateFormat.Is24HourFormat(this); 
var format = is24hour ? "dd/MM/yyyy HH:mm" : "dd/MM/yyyy hh:mm tt"; 
var dtString = date.ToString(format); 
+0

感谢@Malte,它解决了我的问题。 –

1

您可以使用下面的代码你转换日期对象在特定的格式

SimpleDateFormat dateFormat; 
if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) { 
    // 24 hour format 
    dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm"); 
} 
else 
{ 
    // 12 hour format 
    dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); 
} 
String newDate= dateFormat.format(date); 
myTextView.setText(newDate); 
+0

谢谢* * **沙希德。它适用于android。在Xamarin Malte Goetz的解决方案有帮助 –