如何将日期转换为Int? [英] How to Convert Date to Int?

查看:224
本文介绍了如何将日期转换为Int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前值(例如.12)将其分配给变量(例如。今天= 12)。

I'm Trying to get the current day value (example. 12) the assign it to a variable (example. today= 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;


推荐答案

String year1= String.valueOf (year);
String month1= String.valueOf(month);
String day1= String.valueOf(day);

必须更改为

String year1= String.valueOf (num1);
String month1= String.valueOf(num2);
String day1= String.valueOf(num3);

因为这3个变量只保留用户输入的值。

because these 3 variables only hold the values entered by the user.

另外,不确定为什么你需要小时,因为你永远不会从用户那里得到它。在你的情况下,你为所有这些做的是使用 new Date()这样抛出异常。

Also, not sure why you need the hour, because you never get that as inout from the user. In your case, what you've done for all these is use the new Date() as such which is throwing the exception.

你可以用你的小时字段做的最大值是这个

The max you can do with your hour field is this

String hour1 = DateFormat3.format(hour); // Use the current time's hour value as you don't get any from user.

旁注: - 您可以使用单个SimpleDateFormat,单个扫描仪,以及许多其他优化,但是在您修复代码和逻辑之后会出现这种情况。

Side Note:- You can use a single SimpleDateFormat, single Scanner, and a lot of other optimizations, but that comes later, after you fix your code and logic.

这篇关于如何将日期转换为Int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆