简体   繁体   中英

How to split the full date format in date and time?

I have a lot of Strings in the format shown my example that I have to parse. I'm trying to determine which of the Strings are today.

My problem is, that the time is almost there and I just need to compare that date.

Next I want to check if time is between two timestamps "HH:mm:ss" with .after and .before, but there is the problem, that the date is almost there.

How do I split that parsed format in date and time to handle each in its own way?

I'm working in Android Studio, if that's relevant.

String dtStart = "2016-05-23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

     if (new Date().equals(format.parse(dtStart)) ) System.out.println("true");
     else System.out.println("false");

     list.add(new LatLng(lat, lng));

} catch (java.text.ParseException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}

java.time

Use the java.time classes built into Java 8 and later.

Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport , and further adapted to Android in ThreeTen-ABP .

String dateToParse = "2016-05-23 07:24:59";
LocalDateTime dateTime = LocalDateTime.parse(dateToParse, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate localDate = dateTime.toLocalDate();
LocalTime localTime = dateTime.toLocalTime();
// Compare here to your date & time

You can easily achieve it by using the SimpleDateFormat like that:

//Houres - seconds
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

//Years - days
Date hoursAndMinutes = timeFormat.parse(dtStart);
Date yearsMonthsDays = dateFormat.parse(dtStart);

That way, you only get the hours, minutes and seconds of your date. Then, you can do the same for just the year and month and compare it afterwards.

And just to be complete, here's how you'd do it using the Joda date time library and the toLocalDate() and toLocalTime() method.

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime today = new DateTime();
DateTime start = formatter.parseDateTime(dtStart);
if (today.toLocalDate().compareTo(start.toLocalDate()) != 0) {
    System.out.println("true");
} else {
    System.out.println("false");
}

if (today.toLocalTime().compareTo(start.toLocalTime()) > 0) {
...
}

thx for help and sry i forgot to say i'm on android studio..

i found my solution here: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

String dtStart = "2016/05/23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

try {
      Date dtStartOK = format.parse(dtStart);
      String stringDate = DateFormat.getDateTimeInstance().format(dtStartOK);
      System.out.println(stringDate);

      System.out.println(DateFormat.getDateInstance().format(dtStartOK));
      System.out.println(DateFormat.getTimeInstance().format(dtStartOK));

} catch (ParseException e) {
      //Handle exception here, most of the time you will just log it.
      e.printStackTrace();
}

gives me:

23.05.2016 07:24:59

23.05.2016

07:24:59

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM