Java date format real simple

后端 未结 4 2052
生来不讨喜
生来不讨喜 2021-01-24 17:09

how would you write te date if i have a date and all i want is the month and the day like this (mm/dd) and then turn the month like this July, 08

4条回答
  •  渐次进展
    2021-01-24 18:07

    The SimpleDateFormat is your friend here. If you already have a java.util.Date object, just format it using the desired pattern (refer to the javadoc for details on date and time patterns):

    SimpleDateFormat out = new SimpleDateFormat("MMMM, dd");
    String s = out.format(date); // date is your existing Date object here
    

    (EDIT: I'm adding some details as the original question is unclear and I may have missed the real goal.

    If you have a String representation of a date in a given format (e.g. MM/dd) and want to transform the representation, you'll need 2 SimpleDateFormat as pointed out by others: one to parse the String into a Date and another one to format the Date.

    SimpleDateFormat in = new SimpleDateFormat("MM/dd");
    Date date = in.parse(dateAsString); // dateAsString is your String representation here
    

    Then use the code snippet seen above to format it.)

提交回复
热议问题