How to format the Calendar year using 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 s = String.format("Year: %1$tY %1$ty %1$tj", cal);
            System.out.println(s);
            
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
           
run:
     
Year: 2016 16 325
  
 */

 



answered Nov 20, 2016 by avibootz

Related questions

1 answer 258 views
1 answer 220 views
1 answer 186 views
1 answer 206 views
1 answer 239 views
...