如何在Android中格式化date和时间?
如何根据设备configuration格式正确设置年,月,日,时,分的date和时间?
使用标准的Java DateFormat类。
例如要显示当前date和时间,请执行以下操作:
Date date = new Date(location.getTime()); DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); mTimeText.setText("Time: " + dateFormat.format(date)); 你可以用你自己的值初始化一个Date对象,但是你应该知道构造函数已经被弃用了,你应该真的使用一个Java Calendar对象。
 在我看来, android.text.format.DateFormat.getDateFormat(context)让我感到困惑,因为这个方法返回java.text.DateFormat而不是android.text.format.DateFormat –   – “。 
所以,我使用下面的片段代码来获取我的格式的当前date/时间。
 android.text.format.DateFormat df = new android.text.format.DateFormat(); df.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date()); or android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date()); 
另外,您可以使用其他格式。 按照DateFormat 。
date到区域datestring:
 Date date = new Date(); String stringDate = DateFormat.getDateTimeInstance().format(date); 
选项:
  DateFormat.getDateInstance() 
– > 1969年12月31日
  DateFormat.getDateTimeInstance() 
– > 1969年12月31日下午4点00分
  DateFormat.getTimeInstance() 
– > 4:00:00 PM
这将做到这一点:
 Date date = new Date(); java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); mTimeText.setText("Time: " + dateFormat.format(date)); 
使用SimpleDateFormat
喜欢这个:
 event.putExtra("starttime", "12/18/2012"); SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); Date date = format.parse(bundle.getString("starttime")); 
以下是: http : //developer.android.com/reference/android/text/format/Time.html
最好使用Android Native Time类:
 Time now = new Time(); now.setToNow(); 
然后格式化:
 Log.d("DEBUG", "Time "+now.format("%d.%m.%Y %H.%M.%S")); 
这是我的方法,你可以定义和input输出格式。
 public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){ if(inputFormat.equals("")){ // if inputFormat = "", set a default input format. inputFormat = "yyyy-MM-dd hh:mm:ss"; } if(outputFormat.equals("")){ outputFormat = "EEEE d 'de' MMMM 'del' yyyy"; // if inputFormat = "", set a default output format. } Date parsed = null; String outputDate = ""; SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault()); SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault()); // You can set a different Locale, This example set a locale of Country Mexico. //SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX")); //SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX")); try { parsed = df_input.parse(inputDate); outputDate = df_output.format(parsed); } catch (Exception e) { Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage()); } return outputDate; } 
在Time类中使用build!
 Time time = new Time(); time.set(0, 0, 17, 4, 5, 1999); Log.i("DateTime", time.format("%d.%m.%Y %H:%M:%S")); 
的SimpleDateFormat
我使用SimpleDateFormat 而不使用自定义模式,以设备的预选格式从系统获取实际的date和时间:
 public static String getFormattedDate() { //SimpleDateFormat called without pattern return new SimpleDateFormat().format(Calendar.getInstance().getTime()); } 
返回 :
- 13.01.15 11:45
- 1/13/15 10:45 AM
- …
使用这两个作为类variables:
  public java.text.DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); private Calendar mDate = null; 
像这样使用它:
  mDate = Calendar.getInstance(); mDate.set(year,months,day); dateFormat.format(mDate.getTime()); 
此代码将返回当前date和时间:
 public String getCurrDate() { String dt; Date cal = Calendar.getInstance().getTime(); dt = cal.toLocaleString(); return dt; } 
尝试:
 event.putExtra("startTime", "10/05/2012"); 
而当你正在访问传递的variables:
 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date date = formatter.parse(bundle.getString("startTime")); 
我这样使用它:
 public class DateUtils { static DateUtils instance; private final DateFormat dateFormat; private final DateFormat timeFormat; private DateUtils() { dateFormat = android.text.format.DateFormat.getDateFormat(MainApplication.context); timeFormat = android.text.format.DateFormat.getTimeFormat(MainApplication.context); } public static DateUtils getInstance() { if (instance == null) { instance = new DateUtils(); } return instance; } public synchronized static String formatDateTime(long timestamp) { long milliseconds = timestamp * 1000; Date dateTime = new Date(milliseconds); String date = getInstance().dateFormat.format(dateTime); String time = getInstance().timeFormat.format(dateTime); return date + " " + time; } } 
 其他答案通常是正确的。 我想提出现代的答案。 在大多数其他答案中使用的Date , DateFormat和SimpleDateFormat类很久以前就已经过时,并且在很多年以来给许多程序员造成了麻烦。 今天,我们在Java时间和JSR-310这个现代化的Javadate和时间API中有了很多的改进。 你可以在Android上使用它吗? 当然! 在ThreeTenABP项目中,现代课程已被移植到Android。 看到这个问题:如何在Android项目中使用ThreeTenABP了解所有细节。 
这段代码应该让你开始:
  int year = 2017, month = 9, day = 28, hour = 22, minute = 45; LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute); DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); System.out.println(dateTime.format(formatter)); 
当我将电脑的首选语言设置为美国英语或英国英语时,将打印:
 Sep 28, 2017 10:45:00 PM 
当我将其设置为丹麦语时,我得到:
 28-09-2017 22:45:00 
所以它确实遵循configuration。 但是,我无法确切知道设备的date和时间设置有哪些细节,而且这可能因电话而异。
这个代码适合我!
 Date d = new Date(); CharSequence s = android.text.format.DateFormat.format("MM-dd-yy hh-mm-ss",d.getTime()); Toast.makeText(this,s.toString(),Toast.LENGTH_SHORT).show(); 
您可以使用这个(结果取决于手机的默认区域设置,但也可以指定区域设置):
 DateFormat.getDateInstance().format(date) 
==> 3 nov。 2017年
 DateFormat.getDateInstance(DateFormat.SHORT).format(date) 
==> 03/11/2017
 DateFormat.getDateInstance(DateFormat.MEDIUM).format(date) 
==> 3 nov。 2017年
 DateFormat.getDateInstance(DateFormat.LONG).format(date) 
==> 3月11日2017
 DateFormat.getDateInstance(DateFormat.FULL).format(date) 
==> vendredi 3 novembre 2017
 DateFormat.getDateTimeInstance().format(date) 
==> 3 nov。 2017 16:04:58
 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date) 
==> 03/11/2017 16:04
 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date) 
==> 03/11/2017 16:04:58
 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date) 
==> 03/11/2017 16:04:58 GMT + 01:00
 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date) 
==> 03/11/2017 16:04:58 heure normale d'Europe centrale
避免juDate
  Java(和Android)中的Java.util.Date和.Calendar和SimpleDateFormat是非常麻烦的。 避免他们。 他们太糟糕了,Sun / Oracle放弃了他们,用Java 8中的新java.time包取代(而不是2014年的Android)。 新的java.time是由Joda-Time库启发的。 
乔达时间
Joda-Time在Android中工作。
searchStackOverflow的“乔达”find很多的例子和讨论。
使用Joda-Time 2.4源代码的珍闻。
标准格式。
 String output = DateTime.now().toString(); // Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard. 
本地化的格式。
 String output = DateTimeFormat.forStyle( "FF" ).print( DateTime.now() ); // Full (long) format localized for this user's language and culture. 
回到2016年,当我想要自定义格式(不是根据设备configuration,因为你问…)我通常使用string资源文件:
在strings.xml中:
 <string name="myDateFormat"><xliff:g id="myDateFormat">%1$td/%1$tm/%1$tY</xliff:g></string> 
在活动中:
 Log.d(TAG, "my custom date format: "+getString(R.string.myDateFormat, new Date())); 
这对于发布新的date绑定库也很有用。
所以我可以在布局文件中有这样的东西:
 <TextView android:id="@+id/text_release_date" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:padding="2dp" android:text="@{@string/myDateFormat(vm.releaseDate)}" tools:text="0000" /> 
而在java类中:
  MovieDetailViewModel vm = new MovieDetailViewModel(); vm.setReleaseDate(new Date()); 
Android Time类提供了3种格式化方法http://developer.android.com/reference/android/text/format/Time.html
这是我做到的:
 /** * This method will format the data from the android Time class (eg. myTime.setToNow()) into the format * Date: dd.mm.yy Time: hh.mm.ss */ private String formatTime(String time) { String fullTime= ""; String[] sa = new String[2]; if(time.length()>1) { Time t = new Time(Time.getCurrentTimezone()); t.parse(time); // or t.setToNow(); String formattedTime = t.format("%d.%m.%Y %H.%M.%S"); int x = 0; for(String s : formattedTime.split("\\s",2)) { System.out.println("Value = " + s); sa[x] = s; x++; } fullTime = "Date: " + sa[0] + " Time: " + sa[1]; } else{ fullTime = "No time data"; } return fullTime; } 
我希望那有帮助:-)
这太晚了,但对某个人可能有帮助
 DateFormat.format(format, timeInMillis); 
 这里的format是你需要的格式 
例如:“HH:mm”将在15:30返回
语言环境
为了以毫秒为单位获得date或时间的语言环境格式,我使用了这个:
date和时间
 Date date = new Date(milliseconds); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault()); dateFormat.format(date); 
date
 Date date = new Date(milliseconds); DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()); dateFormat.format(date); 
时间
 Date date = new Date(milliseconds); DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()); dateFormat.format(date); 
您可以使用其他date样式和时间样式。 有关这里的样式更多信息。