lonesome
lonesome

Reputation: 2552

Subtract char of digits like normal integers

I have two strings which can be seen as time stamps:

String min="2017-04-15 13:27:31";
String max="2017-04-15 13:40:01";

Assume we want to find out the time passed from first time stamp to the second one. If there was only the time and no date included, I could get it using my following code:

                String[] partsMin=min.split(":");
                String[] partMax=max.split(":");
                int diffZero=Integer.parseInt(partMax[0])-Integer.parseInt(partsMin[0]);                
                int diffOne=Integer.parseInt(partMax[1])-Integer.parseInt(partsMin[1]);
                int diffOTwo=Integer.parseInt(partMax[2])-Integer.parseInt(partsMin[2]);
                diffInSec=diffZero*3600+diffOne*60+diffOTwo;

So here is the question. How to get the job done while there is a date within the time stamp?

Upvotes: 2

Views: 93

Answers (5)

Doing this in your code:

int diffZero=Integer.parseInt(partMax[0])

is the same as doing:

int diffZero=Integer.parseInt("2017-04-15")

that is generating an Exception(NumberFormatException)

you should better try to PARSE those strings min and max into a date

Edit:

you can inspect your code/ variables: and see that splitting to ":" is not giving you back the correct array since the element at index 0 is holding more information than you need...

enter image description here

but as I said before, you are going on the wrong path, dont re invent the wheel and look how practical will get using the APIs that java has for us:

String min = "2017-04-15 13:27:31";
String max = "2017-04-15 13:40:01";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTimeMin = LocalDateTime.parse(min, formatter);
LocalDateTime dateTimeMax = LocalDateTime.parse(max, formatter);
long days = ChronoUnit.DAYS.between(dateTimeMin, dateTimeMax);
long minutes = ChronoUnit.MINUTES.between(dateTimeMin, dateTimeMax);

System.out.println(days);
System.out.println(minutes);

Upvotes: 1

treesong
treesong

Reputation: 303

use SimpleDateFormat to parse the date string, and do operation on Date result, you will get right value. This works well for date between '2017-02-28 23:59:59' and '2017-03-01 00:00:01'

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = format.parse("2017-02-28 23:59:59");
Date date2 = format.parse("2017-03-01 00:00:01");

long time1 = date1.getTime();
long time2 = date2.getTime();

long diff = time2 - time2; // should be 2000

Upvotes: 0

Stimpson Cat
Stimpson Cat

Reputation: 1498

I would construct LocalDateTime instances from it. Then i would get the milliseconds from it and substract startTime from EndTime. What is remaining are the milliseconds passed between the two. A DateTimeFormatter is helpful as well for this purpose.

String strMin = "2017-04-15 13:27:31";
DateTimeFormatter formatterTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTimeMin = LocalDateTime.parse(strMin, formatter);

String strMax = "2017-04-15 13:40:01";
LocalDateTime dateTimeMax = LocalDateTime.parse(strMax, formatter);

long minutes = ChronoUnit.MINUTES.between(dateMin, dateMaxto);
long hours = ChronoUnit.HOURS.between(dateMin, dateMax);

If you want to get the milliseconds:

long millisPassed = dateMax.toEpochMilli() - dateMax.toEpochMilli();

Upvotes: 2

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59960

Did you try with replace all the other part of your String like this :

String[] partsMin = min.replaceAll("\\d+-\\d+-\\d+", "").trim().split(":");
String[] partMax = max.replaceAll("\\d+-\\d+-\\d+", "").trim().split(":");

Upvotes: 1

Tim B
Tim B

Reputation: 41188

Use the java date time libraries (even the old Date class would be fine for this) to parse the string into a proper object.

Depending on the date time library you chose you can then look at the difference between them. The simplest would be something like:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 Date date1 = sdf.parse(str1);
 Date date2 = sdf.parse(str2);
 long differenceInSeconds = (date2.getTime()-date1.getTime())/1000;

The new Java 8 time classes would also allow you to do this and would be better to learn going forwards. I can't remember the syntax for that off the top of my head though.

Upvotes: 1

Related Questions