Last active
June 11, 2018 04:12
-
-
Save hendrawd/9f9b77557b16579c84759c5c699b5343 to your computer and use it in GitHub Desktop.
Yesterday/Today checker for java 8 or https://github.com/ThreeTen/threetenbp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implementation 'com.github.ThreeTen:threetenbp:v1.3.6' | |
// https://github.com/ThreeTen/threetenbp | |
// similar to java8, but support jdk6 and jdk7 | |
import org.threeten.bp.LocalDateTime; | |
import org.threeten.bp.format.DateTimeFormatter; | |
import org.threeten.bp.temporal.ChronoUnit; | |
public class LocalDateTimeYesterdayChecker { | |
public static LocalDateTime getYesterday() { | |
return LocalDateTime.now().minus(1, ChronoUnit.DAYS); | |
} | |
public static boolean isYesterday(LocalDateTime localDateTime) { | |
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; | |
String localDateTimeText = localDateTime.format(formatter); | |
String yesterdayText = getYesterday().format(formatter); | |
return yesterdayText.equals(localDateTimeText); | |
} | |
public static boolean isToday(LocalDateTime localDateTime) { | |
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; | |
String localDateTimeText = localDateTime.format(formatter); | |
String todayText = LocalDateTime.now().format(formatter); | |
return todayText.equals(localDateTimeText); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implementation 'com.github.ThreeTen:threetenbp:v1.3.6' | |
// https://github.com/ThreeTen/threetenbp | |
// similar to java8, but support jdk6 and jdk7 | |
import org.threeten.bp.OffsetDateTime; | |
import org.threeten.bp.format.DateTimeFormatter; | |
import org.threeten.bp.temporal.ChronoUnit; | |
public class OffsetDateTimeChecker { | |
public static OffsetDateTime getYesterday() { | |
return OffsetDateTime.now().minus(1, ChronoUnit.DAYS); | |
} | |
public static boolean isYesterday(OffsetDateTime offsetDateTime) { | |
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE; | |
String offsetDateTimeText = offsetDateTime.format(formatter); | |
String yesterdayText = getYesterday().format(formatter); | |
return yesterdayText.equals(offsetDateTimeText); | |
} | |
public static boolean isToday(OffsetDateTime offsetDateTime) { | |
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; | |
String offsetDateTimeText = offsetDateTime.format(formatter); | |
String todayText = OffsetDateTime.now().format(formatter); | |
return todayText.equals(offsetDateTimeText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment