John Hackle
John Hackle

Reputation: 59

System.out.printf format

I have written down the following code:

public class FractionOfDay {

    public static double fractionOfDay(double h, double m, int s, char a ) {
        double y = -1;
        if (a == 'P' && h == 12) {
            double x = (h * 60 * 60) + (m * 60) + (s);
             y = x / 86400;
        } else if (a == 'P' && h != 12) {
            double x = ( (h + 12) * 60 * 60) + (m * 60) + (s);
            y = x / 86400;
        } else if (a == 'A' && h == 12) {
            double x = (m * 60) + (s);
            y = x / 86400;
        } else if (a == 'A' && h != 12) {
            double x = ( (h) * 60 * 60) + (m * 60) + (s);
            y = x / 86400;
        }
        return y;
    }

    public static void main(String[] args) {
        System.out.println(fractionOfDay(12, 0, 0, 'P'));
    }
}

It prints out the fraction of day which has passed from 12 AM . How can I make it print such a table using printf?enter image description here

Upvotes: 0

Views: 225

Answers (1)

MaxZoom
MaxZoom

Reputation: 7753

Using the Calendar class

public static void main(String[] args) {
  Calendar c = Calendar.getInstance();
  System.out.format("%tl:%tM %tp %10.4f%n", c, c, c, 
    fractionOfDay(
      c.get(Calendar.HOUR),
      c.get(Calendar.MINUTE),
      c.get(Calendar.SECOND),
     (Calendar.AM == c.get(Calendar.AM_PM)) ? 'A' : 'P'));
} 

Upvotes: 1

Related Questions