如何为Android平台正确格式化日期和时间字符串?
这是一些代码:
String path = getFilesDir().getPath();
String filePath = path + "/somefile.xml";
File file = new File(filePath);
Date lastModDate = new Date(file.lastModified());
String filelastModDate = "Updated: " + lastModDate.toString();
String path = getFilesDir().getPath();
String filePath = path + "/somefile.xml";
File file = new File(filePath);
Date lastModDate = new Date(file.lastModified());
String filelastModDate = "Updated: " + lastModDate.toString();
答案 0 :(得分:3)
你可以用各种方式格式化......
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String currentTime = sdf.format(date);
在这里你可以使用其他格式,如
K:毫米
H:毫米
h:mm dd / MM / yyyy
等.....
检查这个.... http://developer.android.com/reference/java/text/SimpleDateFormat.html
答案 1 :(得分:1)
谢谢@receme我解决了它。像这样:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a",Locale.getDefault());
String currentTime = sdf.format(date);
Log.i(LOGTAG,"Current Time: " + currentTime);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a",Locale.getDefault());
String currentTime = sdf.format(date);
Log.i(LOGTAG,"Current Time: " + currentTime);
答案 2 :(得分:1)
Instant.ofEpochMilli( // Parse milliseconds count to a moment in UTC.
file.lastModified() // A count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00:00Z.
) // Returns a `Instant` object.
.atZone( // Adjust from UTC to some time zone. Same moment, same point on the timeline, different wall-clock time.
ZoneId.of( "Africa/Tunis" )
) // Returns a `ZonedDateTime` object.
.format( // Generate a `String`.
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL ) // Specify how long or abbreviated.
.withLocale( Locale.JAPAN ) // Specify a `Local` to determine human language and cultural norms used in localizing.
) // Returns a `String`.
现代方法使用 java.time 类。
File.lastModified
方法返回以1970年1月的UTC 1970-01-01T00:00:00Z的纪元为参考的毫秒数。
long millisSinceEpoch = file.lastModified() ;
将该数字解析为现代的 java.time 对象。
Instant instant = Instant.ofEpochMilli( millisSinceEpoch ) ;
使用标准ISO 8601格式生成String
以表示该值。
String output = instant.toString() ; // Generate a `String` in standard ISO 8601 format.
2018-07-16T22:40:39.937Z
要通过特定区域(某个时区)的人们使用的挂钟时间来查看同一时刻,请应用ZoneId
以获得ZonedDateTime
。
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
2018-07-17T10:40:39.937 + 12:00 [太平洋/奥克兰]
让 java.time 自动本地化。要本地化,请指定:
FormatStyle
来确定字符串应该是多长时间或缩写。Locale
确定:
示例:
Locale l = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( l );
String output = zdt.format( f );
mardi 17 juillet 2018à10:40:39 Heure Normale de laNouvelle-Zélande
java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date
,Calendar
和SimpleDateFormat
。
目前位于Joda-Time的maintenance mode项目建议迁移到java.time类。
要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*
类。
在哪里获取java.time类?
ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如Interval
,YearWeek
,YearQuarter
和more。
答案 3 :(得分:0)
更新:现在位于Joda-Time的maintenance mode项目建议迁移到java.time类。
安装第三方库Joda-Time。
默认情况下,Joda-Time以ISO 8601格式输出字符串。几乎世界上任何人都可以直观地理解这种格式。
在StackOverflow.com上搜索更多示例。
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
DateTime now = new DateTime();
System.out.println( now );
跑步时......
2013-12-05T19:55:43.897-08:00
答案 4 :(得分:0)
我想在这里添加我的分享。
请注意,用户可以在设置中设置其首选格式。如何检索的示例:
static DateFormat getUserDateFormat(Context context) {
if (mUserDateFormat == null)
mUserDateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
return mUserDateFormat;
}
另见...getTimeFormat
然后你有一个java DateFormat用于上面提到的例子。
此外,Android包含它自己的TextFormat类,请看这里:http://developer.android.com/reference/android/text/format/package-summary.html
这可能看起来像:
static String getAppExpiredString() {
String date = android.text.format.DateFormat.getDateFormat(getAppContext()).format(App_Main.APP_RUN_TILL.getTime());
return getAppContext().getString(R.string.app_expired) + " " + date + ".";
}