简体   繁体   English

System.out.printf格式

[英]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 . 它打印出从12 AM开始经过的一天的一部分。 How can I make it print such a table using printf ? 如何使用printf使其打印此类表格? 在此处输入图片说明

Using the Calendar class 使用Calendar

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'));
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM