سؤال

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.

لا يوجد حل صحيح

نصائح أخرى

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.

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

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

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.

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));

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top