【问题标题】:extract year, month and day from a Date type [duplicate]从日期类型中提取年、月和日
【发布时间】:2017-10-28 04:47:13
【问题描述】:

以指定格式输入日期后作为输入。 如何获取日、月和年并将它们包含在三个不同的变量中? 我尝试使用 getYear () 但它给了我一个错误,因为它已被弃用,我读到你可以使用“日历”但我不明白如何在这个库中使用它

System.out.println("Inserisci la data [yyyy-MM-dd]: ");
Scanner in = new Scanner(System.in);
s = in.nextLine();
//converte la stringa della data in un oggetto di classe Date
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
Date d = formato.parse(s);

【问题讨论】:

    标签: java date calendar


    【解决方案1】:
    Calendar c = new Calendar();
    c.setTime(yourDate);
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2015-01-12
      • 2021-10-02
      • 2018-02-16
      • 2019-03-20
      相关资源
      最近更新 更多