A
A
asmodeux2017-07-03 18:21:45
Java
asmodeux, 2017-07-03 18:21:45

Problems with time format in SimpleDateFormat. Where did you mess up?

The application is loaded with a schedule, and there is a method that gives a schedule item depending on the time. For convenience, I did this:

Date date = new Date();
    SimpleDateFormat hour = new SimpleDateFormat("HH");
    String stringHour = hour.format(date);
    int intHour = Integer.parseInt(stringHour);
    SimpleDateFormat minute = new SimpleDateFormat("mm");
    String stringMinute = minute.format(date);
    int intMinute = Integer.parseInt(stringMinute);
    public int hourandminute = (intHour * 100) + intMinute;

Accordingly, I get a variable at the output that turns 07:45 into 745 and so on.
There is also an if , which, depending on the value of hourandminute, gives a specific item. That's only when the time steps over the mark at 13 o'clock - it gives out the item that should have been issued 12 hours ago.
Accordingly, I understand that hour produces in a 12-hour format. Googled a lot, tried to change HH to k, KK and others - the problem is the same. What do I mess with?
Z.Y. Please do not scold too much, I started studying a couple of weeks ago and wanted to practice a little. :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Odissey Nemo, 2017-07-07
@odissey_nemo

Then you should have written:

Date date = new Date();
    SimpleDateFormat hoursmins = new SimpleDateFormat("HHmm");
    String stringHoursMins = hoursmins .format(date);
    int hourandminute = Integer.parseInt(hoursmins );

It's not clear why SimpleDateFormat outputs in 12 hour format. Always and everywhere issued in the format 0-23.
It is also not clear why it is required to multiply hours by 100. It turns out some kind of alien time system. So, instead of our 12 hours 30 minutes = 750 minutes, we get 1230 = 20 hours 30 minutes. But these may already be implementation features or historical features of the account in different sectors of the economy (railway sailors ...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question