我写下了以下代码:
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'));
}
}
它打印出从上午12点开始的一天中的一小部分。
如何使用printf
?
答案 0 :(得分:1)
使用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'));
}