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 188 views
1 answer 245 views
1 answer 204 views
1 answer 224 views
1 answer 209 views
1 answer 181 views
1 answer 122 views
...