How to get the Calendar AM or PM 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("AM or PM? %1$tp", cal);
            System.out.println(s);

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

/*
           
run:
     
AM or PM? pm
  
 */

 



answered Nov 20, 2016 by avibootz

Related questions

2 answers 155 views
1 answer 182 views
1 answer 170 views
170 views asked Aug 15, 2018 by avibootz
1 answer 203 views
1 answer 205 views
1 answer 158 views
158 views asked Oct 29, 2016 by avibootz
...