-1

我如何转换newValue2为,Integer因为我将在 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
4

2 回答 2

1

基于此代码:

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

...看起来很可能newValue2实际上是一些更复杂的日期字符串,因此无法解析为Integer. 由于newValue实际上是一个Date对象,因此您很可能int直接从Date对象中获取您感兴趣的任何日期字段,或者更恰当地使用Calendar

编辑:
根据最近的编辑,这可能是你想要的:

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

PS 如果您使用更清晰、更合理的变量名称,可能会从您的代码中推断出更多的理解。

于 2013-06-06T07:14:41.820 回答
0

基于您的代码newValue2是一个包含来自 的值的字符串SimpleDateFormat.format()。你能显示 的价值newf吗?如果万一这将是所有数字,那么您现在正在做的应该就足够了。

于 2013-06-06T07:16:39.920 回答