Month without leading zeros in Android
Asked Answered
K

5

16

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.

Killdeer answered 30/12, 2011 at 16:32 Comment(1)
Couldn't you just chop off the leading 0 with a substring? Or do you want a more elegant solution?Unspeakable
M
39

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.

Mathia answered 24/1, 2013 at 4:18 Comment(1)
You can also remove one of the h in hours to remove the preceding zero there.Schild
F
4

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
Foppery answered 3/4, 2018 at 1:46 Comment(0)
H
1

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

Highstrung answered 30/12, 2011 at 16:38 Comment(0)
U
1

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.

Uranology answered 1/10, 2016 at 21:16 Comment(0)
M
0

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

Moynihan answered 6/7, 2018 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.