Now getting โtime agoโ in a human-friendly format is pretty easy: String calculateTimeAgoWithPrettyTime(Date pastTime) { PrettyTime prettyTime = new PrettyTime (); return prettyTime.format (pastTime); } 5. Time4J Library. Finally, Time4J is another great library for the manipulation of time and date data in Java.In this case we need to specify a time-of-day to go along with our date-only value, to be combined for a java.util.Date object. First moment of the day likely makes sense. Let java.time determine the time of that first moment as it is not always 00:00:00.0. We also need to specify a time zone, as the date varies by time zone. Add and substract 1 year from LocalDateTime LocalDateTime sameDayNextYear = now.plusYears (1); //2019-07-14 LocalDateTime sameDayLastYear = now.minusYears (1); //2017-07-14. 2. Add or Subtract Days from java.util.Date. Till Java 7, the only good way to add days to Date was using Calendar class. As we can see above, the Date class provides the getMonth() method which returns a number representing the month. Furthermore, we added 1 to the returned number. Then, we called setMonth() to update the Date object with the new month. Notably, itโs always recommended to use the new Date/Time API of Java 8 instead of the old one.
We have a java date object which is in a different timezone then the timezone of machine. We want to date, month and year of the date without changing the timezone. Also the function is unaware of the date's timezone. All it is getting is java date object with date in correct timezone and java Date does not provide Date.getTimeZone().
For a simple way to do it: Calendar cal = new GregorianCalendar(); System.out.println(cal.getTime()); cal.set(Calendar.DAY_OF_MONTH,1); System.out.println(cal.getTime
K4S0.