My linux world » Java – Date

Java - Date


Important : require java 1.8 or greater to use LocalDate.

Contents

Difference beetween two dates

Diff in NanoSeconds

long duration  = endDate.toEpochDay() - startDate.toEpochDay();
long diff = Duration.ofMillis(duration).toNanos();

Diff in MilliSeconds

long diff = endDate.toEpochDay() - startDate.toEpochDay();

Diff in Seconds

long duration  = endDate.toEpochDay() - startDate.toEpochDay();
long diff = Duration.ofMillis(duration).getSeconds();

Diff in Minutes

long duration  = endDate.toEpochDay() - startDate.toEpochDay();
long diff = Duration.ofMillis(duration).toMinutes();

Diff in Hours

long duration  = endDate.toEpochDay() - startDate.toEpochDay();
long diff = Duration.ofMillis(duration).toHours();

Diff in Days

long diff = ChronoUnit.DAYS.between(startDate, endDate);

Diff in Months

long diff = ChronoUnit.MONTHS.between(startDate, endDate);

Diff in Years

long diff = ChronoUnit.YEARS.between(startDate, endDate);

Convert

Convert String to LocalDate

// init stringDate and pattern
String stringDate = "16-08-1979 10:35:00";
String pattern = "dd-M-yyyy hh:mm:ss";
 
// convert to LocalDate
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
LocalDate localDate = LocalDate.parse(stringDate ,dtf);

Convert LocalDate to String

// get current localdate using current zone
LocalDate localDate = LocalDate.now();
 
// set pattern
String pattern = "dd-M-yyyy hh:mm:ss";
 
// convert to String
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
String stringDate = localDate.format(dtf);

Date check

Check if date1 is after date2

boolean result = date1.isAfter(date2);

Check if date1 is before date2

boolean result = date1.isBefore(date2);

Check if date is leap year

boolean result = date.isLeapYear();

Operations

Add days

long daysToAdd = 5;
LocalDate dateUpdated = date.plusDays(daysToAdd);

Add months

long monthsToAdd = 2;
LocalDate dateUpdated = date.plusMonths(monthsToAdd);

Add weeks

long weeksToAdd = 2;
LocalDate dateUpdated = date.plusWeeks(weeksToAdd);

Add years

long yearsToAdd = 2;
LocalDate dateUpdated = date.plusWeeks(yearsToAdd);

Zone

Current zone

ZoneId.systemDefault().getId();

List all available zones

// init result map.
// example "Europe/London" => "UTC+01:00"
Map<String,String> result = new LinkedHashMap<>();
 
// get all zoneIds and add them in an ArrayList
List<String> zoneIds = new ArrayList<>(ZoneId.getAvailableZoneIds());    	
 
Map<String,String> mapNotOrdred = new HashMap<>();
LocalDateTime dt = LocalDateTime.now();
for(String zoneId : zoneIds) {
	ZoneId zone = ZoneId.of(zoneId);
	ZonedDateTime zdt = dt.atZone(zone);
	ZoneOffset zos = zdt.getOffset();
	mapNotOrdred.put(zoneId,zos.getId().replaceAll("Z", "+00:00"));
}
 
//sort by value, descending order
mapNotOrdred.entrySet().stream()
.sorted(Map.Entry.<String, String>comparingByValue().reversed())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
 
// now result map contains all 'zoneIds'=>'UTC' sorted by decending UTC

Copyright © 2023 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.

Fork me on GitHub