【问题标题】:How to convert from Date to Int [duplicate]如何从 Date 转换为 Int [重复]
【发布时间】:2013-10-12 13:39:04
【问题描述】:

我正在尝试获取当前日期值(例如 12),然后将其分配给一个变量(例如今天 = 12)。

    DateFormat DateFormat= new SimpleDateFormat ("dd");

    //get current day time with Date()
    Date day= new Date ();

    int day1 = Integer.valueOf(day);

或者

    DateFormat DateFormat= new SimpleDateFormat ("dd");

    //get current day time with Date()
    Date day= new Date ();

    int day1 = day;

但它没有工作:(

还有其他方法吗?

抱歉,在我的预览问题中没有充分阐明含义:)

【问题讨论】:

  • 你认为你在做什么?你看过Date的javadoc吗?
  • 你的问题标题应该是How to get the day from a Date variable as int?

标签: java int simpledateformat


【解决方案1】:

除了@R.J 答案,您还可以使用Calendar 并获得当天:

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int day = calendar.get(Calendar.DATE);

【讨论】:

    【解决方案2】:

    你可以这样做

    int day1 = Integer.valueOf(DateFormat.format(day));
    

    但我必须说命名约定非常糟糕。

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 2020-07-18
      • 2020-07-08
      • 1970-01-01
      • 2020-11-09
      • 2020-05-13
      • 2015-03-01
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多