2

I want to find the current time and minutes and then convert String to int, so I can use them.

I write this code but Netbeans shows me always this error:

   Exception in thread "main" java.lang.NumberFormatException: For input string: "mm "
   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
   t JavaApplication1.JavaApplication1.main(JavaApplication1.java:66)
   C:\Users\eleft\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:

Java returned: 1 BUILD FAILED (total time: 0 seconds)

    //HOURS   --12 hour form
        Date date1 = new Date();
        String strDateFormat1 = "hh ";
        DateFormat dateFormat1 = new SimpleDateFormat(strDateFormat1);
        String formattedDate1= dateFormat1.format(date1);

        System.out.println("Current time of the day using Date - 12 hour format: " + formattedDate1);

        //MINUTES
        Date date2 = new Date();
        String strDateFormat2 = "mm ";
        DateFormat dateFormat2 = new SimpleDateFormat(strDateFormat2);
        String formattedDate2= dateFormat2.format(date);

        System.out.println("Current time of the day using Date - 12 hour format: " + formattedDate2+ "MINUTES");  


        int current_minutes=Integer.parseInt(strDateFormat2);    

        int current_hours=Integer.parseInt(strDateFormat1);
Jason
  • 11,744
  • 3
  • 42
  • 46
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 04 '19 at 08:46
  • Related: [Java: getMinutes and getHours](https://stackoverflow.com/questions/907170/java-getminutes-and-gethours). I recommend you look at [the answer by J.D.](https://stackoverflow.com/a/29270120/5772882) and [the one by Huey](https://stackoverflow.com/a/44390079/5772882). – Ole V.V. Jan 04 '19 at 09:13

3 Answers3

1

java.time

To get the current hour in 12 hour format and the current minute of the hour as int values:

LocalTime currentTime = LocalTime.now(ZoneId.of("Asia/Qatar"));

int currentHours = currentTime.get(ChronoField.HOUR_OF_AMPM);
int currentMinutes = currentTime.getMinute();

System.out.println("Current time of the day using LocalTime - 12 hour format: "
        + currentHours + " HOURS " + currentMinutes + " MINUTES");

The output when running just now was:

Current time of the day using LocalTime - 12 hour format: 11 HOURS 52 MINUTES

Please substitute your desired time zone where I put Asia/Qatar.

currentTime.get(ChronoField.HOUR_OF_AMPM) gives you the hour from 0 through 11. In case you want 12 instead of 0, just use ChronoField.CLOCK_HOUR_OF_AMPM instead of ChronoField.HOUR_OF_AMPM.

What went wrong in your code?

First you were using the old date-time classes Date and SimpleDateFormat. They are poorly designed and long outdated, so don’t do that. Instead I am using java.time, the modern Java date and time API. You may already have noticed how this also greatly simplifies your code (in combination with the next point).

Second you were formatting the hours and minutes only to parse them back. That’s the detour. Don’t do that.

Third, as the other answers correctly point out, you were trying to parse strDateFormat2, the variable containing the format pattern mm instead of the string containing the number. The exception message you got, For input string: "mm ", is trying to tell you this. Similarly for hours.

Fourth, as Tejas Jagtap correctly says, Integer.parseInt will refuse to parse a string with a trailing space.

Link

Oracle tutorial: Date Time explaining how to use java.time, the modern Java date and time API.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You are parsing the wrong string:

int current_minutes=Integer.parseInt(strDateFormat2);

int current_hours=Integer.parseInt(strDateFormat1);

should be

int current_minutes=Integer.parseInt(formattedDate2);

int current_hours=Integer.parseInt(formattedDate1);

Justin
  • 1,356
  • 2
  • 9
  • 16
0

I figured, the string values have trailing spaces after them, you first need to use trim() to get rid of them. Your code will go like this:

Date date1 = new Date();
    String strDateFormat1 = "hh ";
    DateFormat dateFormat1 = new SimpleDateFormat(strDateFormat1);
    String formattedDate1= dateFormat1.format(date1);

    System.out.println("Current time of the day using Date - 12 hour format: " + formattedDate1);

    //MINUTES
    Date date2 = new Date();
    String strDateFormat2 = "mm ";
    DateFormat dateFormat2 = new SimpleDateFormat(strDateFormat2);
    String formattedDate2= dateFormat2.format(date2);

    System.out.println("Current time of the day using Date - 12 hour format: " + formattedDate2+ "MINUTES");


    int current_minutes=Integer.parseInt(formattedDate2.trim());
    int current_hours=Integer.parseInt(formattedDate1.trim());
Tejas Jagtap
  • 11
  • 1
  • 3