How to format the Calendar second using String.format() in Java

1 Answer

0 votes
package javaapplication1;

import java.io.IOException;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

public class JavaApplication1 {

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

        try {

            Calendar cal = Calendar.getInstance();
            cal.setTime(Date.from(Instant.now()));

            String s = String.format("Second: %1$tS", cal);
            System.out.println(s);

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

/*
           
run:
     
Second: 47
  
 */

 



answered Nov 20, 2016 by avibootz

Related questions

1 answer 187 views
1 answer 240 views
1 answer 200 views
1 answer 219 views
1 answer 204 views
1 answer 177 views
1 answer 120 views
...