β

Java Date and Time Problem

Danny Suen's Blog 3 阅读
 

https://www.hackerrank.com/challenges/java-date-and-time/problem

这道算法题使用Java 8的Date & Time API很容易解决了。

public class Solution {
    private static String getDay(String day, String month, String year) {
        int d = Integer.parseInt(day);
        int m = Integer.parseInt(month);
        int y = Integer.parseInt(year);

        LocalDate date = LocalDate.of(y, m, d);
        return date.getDayOfWeek().toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String month = in.next();
        String day = in.next();
        String year = in.next();

        System.out.println(getDay(day, month, year));
    }
}
 
作者:Danny Suen's Blog
原文地址:Java Date and Time Problem, 感谢原作者分享。

发表评论