如何将字符串转换为整数

ivqmmu1c  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(448)

如何转换 newValue2Integer 因为我要在if语句中使用它?

try {
    toDate=format2.parse(string1);
    java.util.Date newValue= new SimpleDateFormat(oldf).parse(string1);
     String newValue2 = new SimpleDateFormat(newf).format(newValue);

     int qwe = Integer.parseInt(newValue2);

     if (qwe < 8){

            String fixtime = ("08:00 PM");

            DateTime dateTime3 = dtf.parseDateTime(fixtime.toString());
            Period period = new Period(dateTime3, dateTime2);

            PeriodFormatter formatter = new PeriodFormatterBuilder()

            .appendHours().appendSuffix(".")
            .appendMinutes().appendSuffix("")
            .toFormatter();

            String elapsed = formatter.print(period);

            table_4.setValueAt(elapsed,0,3);
                        }

    } catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

我尝试修改它只是为了看到 newValue2 在这里:

try {
    toDate=format2.parse(string1);
    java.util.Date newValue= new SimpleDateFormat(oldf).parse(string1);
    String newValue2 = new SimpleDateFormat(newf).format(newValue);

    System.out.println (newValue2);

//  int qwe = Integer.parseInt(newValue2);

//  System.out.println(qwe);

    } catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

要输出的数据示例:

07:00 AM 

System.out.println(newValue2);

07.00 0
vd2z7a6w

vd2z7a6w1#

基于你的代码 newValue2 是一个字符串,其中包含 SimpleDateFormat.format() . 你能告诉我这个东西的价值吗 newf ? 如果它将是所有的数字,那么你现在所做的应该是足够的。

hfsqlsce

hfsqlsce2#

基于此代码:

String newValue2 = new SimpleDateFormat(newf).format(newValue);
 int qwe = Integer.parseInt(newValue2);

... 似乎很有可能 newValue2 实际上是一些更复杂的日期字符串,因此无法解析为 Integer . 自 newValue 实际上是一个 Date 对象,你很可能只得到你感兴趣的日期字段 int 直接从日期对象或更恰当地使用日历。
编辑:
根据最近的编辑,这可能是您想要的:

Calendar calendar = Calendar.getInstance();
calendar.setTime(newValue);
int qwe = calendar.get(Calendar.HOUR);

p、 如果您使用更清晰、合理的变量名,则可以从代码中推断出更多的理解。

相关问题