NewJava :
Given a date and time in string format like "20 10 50", I have successfully converted it into an SDF by referring the internet. Now how do I operate on the same date I just created?
X=3600*HH+60*MM+SS
public static void main(String[] args) throws ParseException {
int numberOfTests=3;
String [] timeStamps=new String[3];
timeStamps[0]="10 10 10";
timeStamps[1]="10 10 16";
timeStamps[2]="10 10 50";
int [] X=new int[3];
DateFormat sdf = new SimpleDateFormat("hh mm ss");
Date t1=sdf.parse(timeStamps[0]);
Date t2=sdf.parse(timeStamps[1]);
Date t3=sdf.parse(timeStamps[2]);
//X=3600*HH+60*MM+SS
This is how far I have reached. I know what I have to do after this, I've got it all sorted, but I am getting stuck here.
I tried to do it using split
at first, but it became complex later on, so I stuck to this method.
azro :
If you just want to apply X = 3600*HH + 60*MM + SS
you don't need to pass through a date (either Date
or LocalTime
) just use the String
for (int i = 0; i < timeStamps.length; i++) {
String[] parts = timeStamps[i].split(" ");
X[i] = Integer.parseInt(parts[0]) * 3600 + Integer.parseInt(parts[1]) * 60 + Integer.parseInt(parts[2]);
}
System.out.println(Arrays.toString(X)); // [36610, 36616, 36650]
To see an example with a parsing, you'll see that you get the same but more costly with a parsing to LocalTime
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH mm ss");
for (int i = 0; i < timeStamps.length; i++) {
LocalTime time = LocalTime.parse(timeStamps[i], fmt);
X[i] = time.getHour() * 3600 + time.getMinute() * 60 + time.getSecond();
}
System.out.println(Arrays.toString(X)); // [36610, 36616, 36650]
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=389124&siteId=1