Android 中没有前导零的月份

发布于 2024-12-24 16:15:21 字数 262 浏览 1 评论 0原文

有没有办法在 Java/Android 中格式化没有前导零的月份?

我得到了这个:

mTitleText.setText(String.format("Pick Date Time: %tm/%te/%tY %tk:%02d",
                mTime,mTime,mTime,mTime,minute)); 

当我希望它返回 2/12/2012 13:23 时,它返回 02/12/2012 13:23。

Is there a way to format a Month without leading zeros in Java/Android?

I got this:

mTitleText.setText(String.format("Pick Date Time: %tm/%te/%tY %tk:%02d",
                mTime,mTime,mTime,mTime,minute)); 

And it returns 02/12/2012 13:23 when I want it to return 2/12/2012 13:23.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风向决定发型 2024-12-31 16:15:21

对于那些有兴趣避免冗长的 JavaDocs 的人:

Date mTime = new Date();  
String text = new SimpleDateFormat("M/d/yyyy hh:mm").format(mTime);

使用 M 代替 MMd 代替 dd 将渲染月份和月份中的日期,且不带前导零如果可能的话。

For those interested in avoiding the lengthy JavaDocs:

Date mTime = new Date();  
String text = new SimpleDateFormat("M/d/yyyy hh:mm").format(mTime);

Using M instead of MM and d instead of dd will render the day of the month and month without leading zeros if possible.

ˉ厌 2024-12-31 16:15:21

使用Java 8 DateTimeFormatter,它可以通过使用单个字符而不是用于表示每个日、月、小时、分钟和秒的字符对来实现。

“yyyy/M/d H:m:s” 将允许解析日期和时间,而无需零填充值。

“yyyy/MM/dd HH:mm:ss” 需要(期望)零填充,否则将抛出 DateTimeParseException

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

//start class and method scope
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m");
LocalDateTime.parse(stringToBeParsed, formatter);
} catch (DateTimeParseException e) {
//Log error in parsing, exit program, abort current execution, return null response for API call, shutdown system, whatever.
}
//end class and method scope

Using Java 8 DateTimeFormatter, it can be achieved by using single characters instead of the pairs used to denote each of day, month, hour, minutes and seconds.

"yyyy/M/d H:m:s" will allow parsing date and time without zero padded values.

"yyyy/MM/dd HH:mm:ss" requires(expects) zero-padding else will throw a DateTimeParseException.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

//start class and method scope
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m");
LocalDateTime.parse(stringToBeParsed, formatter);
} catch (DateTimeParseException e) {
//Log error in parsing, exit program, abort current execution, return null response for API call, shutdown system, whatever.
}
//end class and method scope
鸠书 2024-12-31 16:15:21

您可以使用 SimpleDateFormat 类来代替使用 String.format() 吗?它将执行您想要的操作,但是如果没有看到更多代码,我无法判断您是否有可以使用的日期或日历对象。

http://docs.oracle.com/javase/6 /docs/api/java/text/SimpleDateFormat.html

Instead of using String.format(), can you use the SimpleDateFormat class? It will do what you want, but without seeing more of your code I can't tell if you have a Date or Calendar object you can use.

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

┈┾☆殇 2024-12-31 16:15:21

tl;dr

ZonedDateTime.now()
             .format( DateTimeFormatter.ofPattern( "M/d/uuuu HH:mm" ) )

java.time

现代方法是使用 java.time 类来取代麻烦的旧遗留日期时间类。

DateTimeFormatter 类,使用月份和/或月份中的单字符代码而不是双字符代码定义格式化模式。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/d/uuuu HH:mm" );

获取当前时刻。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );

生成一个字符串。

String output = zdt.format( f )

关于 java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧日期时间类,例如 java.util.Date.Calendar, & java.text.SimpleDateFormat

Joda-Time 项目,现已在 维护模式,建议迁移到 java.time。

要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。

许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7 并进一步适应 AndroidThreeTenABP(请参阅如何使用...) 。

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 间隔YearWeekYearQuarter更多

tl;dr

ZonedDateTime.now()
             .format( DateTimeFormatter.ofPattern( "M/d/uuuu HH:mm" ) )

java.time

The modern way is with the java.time classes that supplant the troublesome old legacy date-time classes.

In the DateTimeFormatter class, define a formatting pattern using single character codes for month and/or day-of-month rather than double-character codes.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/d/uuuu HH:mm" );

Get the current moment.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );

Generate a string.

String output = zdt.format( f )

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

千鲤 2024-12-31 16:15:21

SimpleDateFormat 很好,但你也可以这样做:


mTitleText.setText(String.format("选择日期时间:%s/%1$te/%$1tY %$1tk:%02d",
mTime.getMonth()+1,mTime,分钟));

SimpleDateFormat is good, but you can also do it like this:


mTitleText.setText(String.format("Pick Date Time: %s/%1$te/%$1tY %$1tk:%02d",
mTime.getMonth()+1,mTime,minute));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文