3
System.out.printf("Time: %d-%d %02d:%02d"    +            
 calendar.get(Calendar.DAY_OF_MONTH),
 calendar.get(Calendar.MONTH),
 calendar.get(Calendar.HOUR_OF_DAY), 
 calendar.get(Calendar.MINUTE);

That is the code a friend showed me, but how do I get the date to appear in a Format like November 1?

user2738201
  • 53
  • 1
  • 7
  • 4
    Use a Date object together with a SimpleDateFormat. – Hovercraft Full Of Eels Nov 02 '14 at 02:00
  • It's a pain... SDF is a crap and so is `printf`. I wrote an utility allowing to write `myFormatter.format("Time: [d-m HH:MM]", new Date())` and allowing to plug in other formatting strings for the same or different types, but it's not published yet. – maaartinus Nov 02 '14 at 15:18
  • @maaartinus If you could elaborate on the issues you see with SDF in an answer, I believe it would be useful to many readers. Otherwise the OP will leave thinking only "somebody said SDF was crap but I don't know why" (and personally I disagree) and risks avoiding a good tool for no real reason. – Jason C Nov 02 '14 at 15:27
  • @JasonC You're right... though this theme deserves a length rant more than a SO answer. I wrote an answer concerning `printf`. It's a pain that we got a poor C function remake.`SDF` was not the subject of the question, but my concerns with it are simple: A class which is *both* non-thread-safe and costly to create is a terrible combination. – maaartinus Nov 02 '14 at 15:48
  • possible duplicate of [How to get month name from Calendar](http://stackoverflow.com/questions/14832151/how-to-get-month-name-from-calendar) – Basil Bourque Nov 02 '14 at 16:26

4 Answers4

2

This is how to do it:

DateFormat dateFormat = new SimpleDateFormat( "MMMMM d" );
Calendar calendar = new GregorianCalendar(); // The date you want to format
Date dateToFormat = calendar.getTime();
String formattedDate = dateFormat.format( dateToFormat );
System.out.println( formattedDate );
Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49
1
Date d = new Date();
System.out.printf("%s %tB %<td", "Today",  d);
// output : 
//   Today november  01
  • %tB for Locale-specific full month name, e.g. "January", "February".
  • %<td d for Day of month, formatted as two digits with leading zeros as necessary, < for reuse the last parameter.
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
1

The DateFormat answer is the way to do this. The printf answer is also good although does not provide locale-specific formats (it provides language-specific names but does not use e.g. the day/month/year ordering that the current locale uses).

You asked in a comment:

Can I do it with the calendar.get(Calendar.MONTH) etc method? Or do I have to use date format?

You don't have to use the other methods here, but if you want to use the Calender fields, it is up to you to convert the numeric values they provide to strings like "Tuesday" or "November". For that you can use the built in DateFormatSymbols, which provides internationalized strings from numbers for dates, in the form of String arrays, which you can use the Calendar fields to index in to. See How can I convert an Integer to localized month name in Java? for example.

Note you can use DateFormat.getDateInstance() to retrieve a pre-made format for the current locale (see the rest of those docs, there are also methods for getting pre-made time-only or date+time formats).

Basically you have the following options:

  • DateFormat (SimpleDateFormat for custom formats)
    • Locale-specific format (e.g. day/month/year ordering): Yes
    • Language-specific names (e.g. English "November" vs. Spanish "Noviembre"): Yes
    • Does the work for you: Yes. This is the best way and will provide a format that the user is used to working with, with no logic needed on your end.
  • printf date fields
    • Locale-specific format: No
    • Language-specific names: Yes
    • Does the work for you: Partly (up to you to determine field ordering)
  • Calendar fields with DateFormatSymbols
    • Locale-specific format: No
    • Language-specific names: Yes
    • Does the work for you: No
  • Calendar fields with your own string conversions (like a big switch statement):
    • Locale-specific format: No
    • Language-specific names: No
    • Does the work for you: No

Another advantage of DateFormat-based formats vs printf date fields is you can still define your own field ordering and formats with the SimpleDateFormat (just like printf) but you can stick to the DateFormat interface which makes it easier to pass around and combine with stock date formats like DateFormat.getDateInstance(DateFormat.MEDIUM).

Check out the documentation for DateFormat for info on the things you can do with it. Check out the documentation for SimpleDateFormat for info on creating custom date formats. Check out this nice example of date formats (archive) for some example output if you want instant gratification.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
1

There's a direct way how to do it using printf, but it's a pain, too:

String.printf("Time: %1$td-%1$tm %1$tH:%1$tM", new Date());

One problem with it is that it uses 4 formatting strings with the same object, so it needs the 1$ prefix to always access the first argument. The other is that I can never remember what letter means what (but maybe that's just me).

Speed could actually be another problem, if you care.

This is documented in the underlying class Formatter.


My preffered way would be something like

myFormatter.format("Time: [d-m HH:MM]", new Date())

where the braces would save us from repeating $1 and make clear where the argument ends.

maaartinus
  • 44,714
  • 32
  • 161
  • 320