2

I have written a small program in which a user enters minutes and program shows the current Date and Time + minutes entered by the user.

final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, Integer.valueOf(sample.getMinutes()));

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String dt = sdf.format(cal.getTime());

System.out.println(" Date and time with added Minutes : " + (dateFormat.parse(dt));

Sample

private String minutes;

//getter and setter

I am getting this exception

java.lang.NumberFormatException: For input string: ""

What I am doing wrong here? Should I use

Integer.parseInt

or

Integer.valueOf(Integer.parseInt(sample.getMinutes())));?
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Namita
  • 779
  • 4
  • 12
  • 24
  • What is `sample`? The `NumberFormatException` happens because you have an empty string `""` that you're trying to parse as a number, but an empty string cannot be sensibly parsed as a number. `sample.getMinutes()` returns an empty string. – Jesper Feb 09 '12 at 11:52
  • have u initialized the sample variable? – xyz Feb 09 '12 at 11:52

9 Answers9

1
  1. With current date and time.

    public static void main(String[] args) 
    {
        try 
        {
            final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
            final DateFormat dateFormat  = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
    
            Date sample  = new Date();
            int iMinutes = 30;//minutes added by the user
    
            Calendar cal = Calendar.getInstance();
    
            cal.add(Calendar.MINUTE, Integer.valueOf(sample.getMinutes()));
    
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String dt            = sdf.format(cal.getTime());
    
            System.out.println("Current Date and time:"+sample);
            System.out.println("Date and time with added Minutes : " + (dateFormat.parse(dt)));
        } 
        catch (ParseException ex) 
        {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    The output will be displays as:
    Current Date and time:Tue Jun 12 15:57:55 IST 2012
    Date and time with added Minutes : Tue Jun 12 16:54:55 IST 2012
    Here the minutes "57" was added to the calendar and the time has moved forward by "30" mins.And that is the your result(Date and time with added Minutes).

  2. With user in input minutes.

    public static void main(String[] args) 
    {
        try 
        {
            final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
            final DateFormat dateFormat  = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
    
            int iMinutes = 30;//minutes added by the user
    
            Calendar cal = Calendar.getInstance();
    
            cal.add(Calendar.MINUTE, iMinutes);
    
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            String dt            = sdf.format(cal.getTime());
    
            System.out.println("Current Date and time:"+sample);
            System.out.println("Date and time with added Minutes : " + (dateFormat.parse(dt)));
        } 
        catch (ParseException ex) 
        {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    This will work as per your desire first you take the minutes from the user and assign that minutes to the "iMinutes" variable of the code it will add that much minutes to the calander.

    The output will be displayed as:
    Current Date and time:Tue Jun 12 16:07:55 IST 2012
    Date and time with added Minutes : Tue Jun 12 16:37:55 IST 2012

And if you want to set the minutes then use "set" instead of "add" in the "cal.add".

Hope this will solve your problem.
Regards.

user1397872
  • 49
  • 2
  • 9
1

Check if the returned string from sample.getMinutes() is a number or not. It must be a number without any white space to be parsed, otherwise you will get a NumberFormatException.

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
0

The problem you're having is that an empty string is not a valid integer. Your application should catch the exception, and set a sensible default instead.

David Grant
  • 13,929
  • 3
  • 57
  • 63
0

"" is an empty string and it cannot be parsed into a valid integer given any circumstances

Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
0

java.lang.NumberFormatException: For input string: ""

An empty String cannot be parsed to a number.

You need to check it first (using something like String#length() or StringUtils#isBlank()) and decide what to do with this case (for example treat it as zero).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

java.lang.NumberFormatException: For input string: ""

Seems like you never set the minutes String

Matthias Bruns
  • 899
  • 8
  • 13
0

java.lang.NumberFormatException: For input string: ""

input String "" can not be converted into valid Integer.

before using Integer.parseInt, you ensure you are getting an integer by the following ways.

1.provide javascript validation for checking int

or/and

2.provide a server side validation for checking non-integer Strings

also see how to avoid NumberFormatException

Community
  • 1
  • 1
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
0

Add some sort of checking:

final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);

Calendar cal = Calendar.getInstance();
final String minutes = sample.getMinutes()
cal.add(Calendar.MINUTE, Integer.valueOf((minutes != null && !minutes.isEmpty()) ? minutes : 0);

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String dt = sdf.format(cal.getTime());

System.out.println(" Date and time with added Minutes : " + (dateFormat.parse(dt));
kornero
  • 1,109
  • 8
  • 11
0

There is nothing unusal here. Read the java docs for parseInt() and valueOf which clearly states that a NumberFormatException is thrown if the String does not contain a parsable integer. And an empty string "" is not a parsable int.

It is up to you how you handle such cases for which a NumberFormatException is thrown.

Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73