TL; DR
ZonedDateTime.now() .format( DateTimeFormatter.ofPattern( "M/d/uuuu HH:mm" ) )
java.time
The modern way is the java.time classes, which supersede the nasty old obsolete time classes.
In the DateTimeFormatter class, define a formatting pattern using separate character codes for the month and / or day of the month, rather than two-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 );
Create a line.
String output = zdt.format( f )
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .
The Joda-Time project, now in maintenance mode , advises switching to java.time.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.
Most java.time functions return to Java 6 and 7 in ThreeTen-Backport and are further adapted to Android in ThreeTenABP (see How to use ... ).
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .
source share