How to format a date in java?

前端 未结 4 2000
深忆病人
深忆病人 2020-12-20 10:37

How can change this date format \"2011-09-07T00:00:00+02:00\" into the \"dd.MM.\" i.e \"07.09.\"

Thanks in advance!

相关标签:
4条回答
  • 2020-12-20 11:03

    Your code needs a little correction on the following line

    Date date = inputDf.parse(input.substring(0,9));
    

    In place of (0,9) you need to enter (0,10) and you will be getting the desired output.

    0 讨论(0)
  • 2020-12-20 11:06

    Basically -

    1. Create a date format object from the above string

    2. Parse into a date object, and reformat however you prefer.

    For example (I haven't tested this):

    /*
     * REFERENCE:
     * http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/
     */
    import java.text.DateFormat;
    import java.util.Date;
    
    public class DateFormatExample1 {
    
        public static void main(String[] args) {
            // Make a new Date object. It will be initialized to the current time.
            DateFormat dfm = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
            Date d = dfm.parse("2011-09-07 00:00:00");
    
            // See what toString() returns
            System.out.println(" 1. " + d.toString());
    
            // Next, try the default DateFormat
            System.out.println(" 2. " + DateFormat.getInstance().format(d));
    
            // And the default time and date-time DateFormats
            System.out.println(" 3. " + DateFormat.getTimeInstance().format(d));
            System.out.println(" 4. " +
                DateFormat.getDateTimeInstance().format(d));
    
            // Next, try the short, medium and long variants of the
            // default time format
            System.out.println(" 5. " +
                DateFormat.getTimeInstance(DateFormat.SHORT).format(d));
            System.out.println(" 6. " +
                DateFormat.getTimeInstance(DateFormat.MEDIUM).format(d));
            System.out.println(" 7. " +
                DateFormat.getTimeInstance(DateFormat.LONG).format(d));
    
            // For the default date-time format, the length of both the
            // date and time elements can be specified. Here are some examples:
            System.out.println(" 8. " + DateFormat.getDateTimeInstance(
                DateFormat.SHORT, DateFormat.SHORT).format(d));
            System.out.println(" 9. " + DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM, DateFormat.SHORT).format(d));
            System.out.println("10. " + DateFormat.getDateTimeInstance(
                DateFormat.LONG, DateFormat.LONG).format(d));
        }
    }
    
    0 讨论(0)
  • 2020-12-20 11:10

    tl;dr

    OffsetDateTime.parse( "2011-09-07T00:00:00+02:00" ).format( DateTimeFormatter.ofPattern( "dd.MM" )
    

    java.time

    The Question and other Answers use old legacy classes that have proven to be troublesome and confusing. They have been supplanted by the java.time classes.

    Your input string is in standard ISO 8601 format. These formats are used by default in java.time classes. So no need to specify a formatting pattern.

    OffsetDateTime odt = OffsetDateTime.parse( "2011-09-07T00:00:00+02:00" );
    

    You can generate a String in your desired format.

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM" );
    String output = odt.format( f );
    

    MonthDay

    You want month and day-of-month. There is actually a class for that, MonthDay.

    MonthDay md = MonthDay.from( odt );
    

    You can generate a String in your desired format.

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM" );
    String output = md.format( f );
    
    0 讨论(0)
  • 2020-12-20 11:24

    here is a sample

    edited the code:

    public static void main(String[] args) throws ParseException {
        String input = "2011-09-07T00:00:00+02:00";
        SimpleDateFormat inputDf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat outputDf = new SimpleDateFormat("dd.MM");
    
        Date date = inputDf.parse(input.substring(0,9));
        System.out.println(date);
        System.out.println(outputDf.format(date));
    }
    
    0 讨论(0)
提交回复
热议问题