Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. // SimpleDateFormat code12Hours = new SimpleDateFormat("HH:mm"); // 24 hour format
  10.  
  11. SimpleDateFormat code12Hours = new SimpleDateFormat("hh:mm"); // 12 hour format
  12.  
  13. Date dateCode12 = null;
  14.  
  15. String formatTwelve;
  16. String results;
  17.  
  18. String[] date = {"01:11","23:11","13:12","07:01", "00:00", "12:00", "11:59"};
  19.  
  20. for (String res: date) {
  21.  
  22. try {
  23. dateCode12 = code12Hours.parse(res); // 12 hour
  24. } catch (ParseException e) {
  25. e.printStackTrace();
  26. }
  27.  
  28. formatTwelve = code12Hours.format(dateCode12); // 12
  29.  
  30. if (formatTwelve.equals(res)) {
  31. results = formatTwelve + " AM";
  32. } else {
  33. results = formatTwelve + " PM";
  34. }
  35.  
  36. System.out.println(res + " : " + results);
  37.  
  38. }
  39.  
  40. }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement