Skip to content

Instantly share code, notes, and snippets.

@hendrawd
Last active June 11, 2018 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrawd/9f9b77557b16579c84759c5c699b5343 to your computer and use it in GitHub Desktop.
Save hendrawd/9f9b77557b16579c84759c5c699b5343 to your computer and use it in GitHub Desktop.
Yesterday/Today checker for java 8 or https://github.com/ThreeTen/threetenbp
// 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);
}
}
// 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