How to format the Calendar month, day and year into a string with String.format() in Java

1 Answer

0 votes
package javaapplication1;

import java.io.IOException;
import java.util.Calendar;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            Calendar cal = Calendar.getInstance();
            cal.set(2016, 10, 20);

            String result = String.format("Month: %1$tB Day: %1$te Year: %1$tY", cal);
            System.out.println(result);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
           
run:
     
Month: November Day: 20 Year: 2016
  
 */

 



answered Nov 20, 2016 by avibootz
...