How to display the current time in 12 hour format with AM/PM in Java

2 Answers

0 votes
import java.text.SimpleDateFormat;  
import java.util.Date;  
 
public class MyClass {
    public static void main(String args[]) {

        // h - Hour in am/pm (1-12)
        // mm - Minute in hour
        // a - AM/PM

        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");  
        
        Date date = new Date();  
        
        String str = formatter.format(date);  
         
        System.out.println(str);  
    }
}
  
  
  
  
/*
run:
  
08:39 AM
  
*/

 



answered Oct 27, 2023 by avibootz
0 votes
import java.text.SimpleDateFormat;  
import java.util.Calendar;
  
public class MyClass {
    public static void main(String args[]) {
 
        // h - Hour in am/pm (1-12)
        // mm - Minute in hour
        // a - AM/PM
 
        Calendar calendar = Calendar.getInstance();
        
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");
    
        System.out.println(formatter.format(calendar.getTime()));
    }
}
   
   
   
   
/*
run:
   
04:18 PM
   
*/

 



answered Nov 8, 2023 by avibootz

Related questions

1 answer 184 views
1 answer 155 views
155 views asked Oct 29, 2016 by avibootz
1 answer 198 views
1 answer 200 views
1 answer 127 views
...