我如何将String转换为int? [英] How i convert String to int?

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

问题描述

这是Code,我试图将Minutes(String类型)转换为整数类型,但我有NumberFormatException错误,任何人都可以帮助我,我如何处理这种情况。谢谢。

Here's Code, I am trying to convert Minutes which is String type, to integer type but I am having NumberFormatException error, can anyone help me, how i handle this situation. Thanks.

import java.util.Date;

class DateDemo 
{
    public static void main(String args[]) 
    {
        // Instantiate a Date object
        Date date = new Date();

        // display time and date
        String str = String.format("Current Minutes : %tM", date );
        try
        {
            int a = Integer.valueOf(str);
            System.out.print(a);

        }
        catch(NumberFormatException e)
        {
            System.out.println("Error is : "+e);
        }
    }
}


推荐答案

使用

String str = String.format("%tM", date );

您得到异常,因为您尝试将字符串当前日期/时间:xx转换为数字;

You get the exception because you try to convert a String "Current Date/Time : xx" to a number;

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

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