Based on How to convert Standard Date into minutes in java? information, I follow this technique to convert date into minute, but an output is not as an expected value (e.g., 12 hour -> it should be 720 minutes).
Here is my source code
SimpleDateFormat sdf = new SimpleDateFormat("hh");
Date theDate = sdf.parse ("12");
long minutes22 = (theDate.getTime() / 1000) / 60;
System.out.println("Minute "+minutes22);
An output is -420. Can someone explain to me ?
< First problem is solved, please take a look at the comment section >
The new problem is cutting String. < The problem is solved as well,please take a look at comment section >
For Another Input that I have try to do my own solution,but there is problem occur after finishing cutting String. I found that duplicate value can not keep separately. I don t know why would happen because I kept in different index. Any suggestion ?
Here is another my another source code.
String date = "Sun 10:00-20:00\r\n" +
"Fri 05:00-10:00\r\n" +
"Fri 16:30-23:50\r\n" +
"Sat 10:00-24:00\r\n" +
"Sun 01:00-04:00\r\n" +
"Sat 02:00-06:00\r\n" +
"Tue 03:30-18:15\r\n" +
"Tue 19:00-20:00\r\n" +
"Wed 04:25-15:14\r\n" +
"Wed 15:14-22:40\r\n" +
"Thu 00:00-23:59\r\n" +
"Mon 05:00-13:00\r\n" +
"Mon 15:00-21:00";
List<String> Hour = new ArrayList<String>();
int loop = 0;
String [] replacement = {"sun","mon","tue","wed","thu","fri","sat"};
String new_s = date.toLowerCase().replaceAll(" ","");
for(int j = 0;j<replacement.length;j++)
{
new_s = new_s.replace(replacement[j],"").replace("\r\n","");
}
String temp = "";
temp = temp+new_s.replaceAll("-","");
int show = 0;
while(!temp.equals(""))
{
Hour.add(temp.substring(loop,loop+5));
temp = temp.replace(temp.substring(0,loop+5),"");
System.out.println("Hour "+Hour.get(show));
show++;
}
Here is the output
Hour 10:00
Hour 20:00
Hour 05:00
Hour 16:30
Hour 23:50
Hour 24:00
Hour 01:00
Hour 04:00
Hour 02:00
Hour 06:00
Hour 03:30
Hour 18:15
Hour 19:00
Hour 04:25
Hour 15:14
Hour 22:40
Hour 00:00
Hour 23:59
Hour 13:00
Hour 15:00
Hour 21:00
Another question - why would it print out < 0 instead of 1200 >
LocalTime froms = LocalTime.parse("20:00", DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH));
System.out.println("TIme "+froms.getMinute());'