How to convert given days into year, months, days in java [duplicate] - java

This question already has answers here:
Adding days to a date in Java [duplicate]
(6 answers)
Closed 6 years ago.
How do I convert given days into Calendar format in java.
Example Initial date is 01-01-2015. Given days are 125 days. This should be converted as 0 years, 4 months, 5 days and added to initial date which would become 06-05-2015.

You can use Period class from java8's new java.time API to convert the difference between two dates into years, months and days:
LocalDate initial = LocalDate.of(2015, 1, 1);
LocalDate end = initial.plusDays(125);
Period p = Period.between(initial, end);
int years = p.getYears(); // 0
int months = p.getMonths(); // 4
int days = p.getDays(); // 5

Related

Android/Java Difference between 2 dates in days [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 12 months ago.
So I'm having some issues in trying to do this, I already tried with this code:
thatDay.set(Calendar.DAY_OF_MONTH,25);
thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 1985);
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
And I kinda know why this is not what I want because, the thing that I need is taking the first date that I insert in database, and I want that to be like (CURRENTDATE - TXTDATE == DAYS THAT LEFT)
Maybe try this:
public long daysBetween(Calendar startDate, Calendar endDate) {
return TimeUnit.MILLISECONDS.toDays(Math.abs(endDate.getTimeInMillis() - startDate.getTimeInMillis()));
}

change date 5 digits int to date string in java [duplicate]

This question already has answers here:
Converting number representing a date in Excel to a Java Date object
(6 answers)
Closed 4 years ago.
Excel converts dates into 5 digits int so I'm asking for a way to convert this 5 digits int ex. 43277 to date String dd-MMM-yy and how to change it "date String" back to 5 digits int.
I'm trying to make the app reads and writes from an google spreadsheet.
It's true that a similar question asked here "Converting Number representation of Date in excel to Date in java" but the code provided did not work or me as it showed cannot resolve symbol 'DateUtil' and the i couldnt understand the second answer.
Also I am asking how to convert it back and forth not from int to String only.
Excel’s serialized dates are the number of days since 1/1/1900. In order to figure out the date again, we have to add the serial number worth of days.
Java 8 version
/*
1900-1-0 0
1900-1-1 1
1900-1-2 2
1900-1-3 3
*/
int days = 43323;
LocalDate start = LocalDate.of(1900, 1, 1);
LocalDate today = LocalDate.of(2018, 8, 11);
// days to date
LocalDate date = start.plusDays(days).minusDays(2);
System.out.println(date);
// date to days
long days1 = ChronoUnit.DAYS.between(start, today) + 2;
System.out.println(days1);

Java 8 Date time for calculating age in decimals [duplicate]

This question already has answers here:
Java Time period in decimal number of years
(3 answers)
Closed 5 years ago.
I am new in using Java 8 date time API and was wondering how can i able to calculate the age in decimals which returns the double value like 30.5 which means 30 years and 6 months? For example the below sample code gets me the output as 30.0 but not 30.5 which probably am trying for.
LocalDate startDate = LocalDate.of(1984, Month.AUGUST, 10);
LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 10);
double numberOfYears = ChronoUnit.YEARS.between(startDate, endDate);
System.out.println(numberOfYears); //Getting output as 30.0 but not 30.5
The JavaDoc for ChronoUnit's between method clearly states:
The calculation returns a whole number, representing the number of complete units between the two temporals.
So you can't get 30.5 by just querying for years between. It only will give you the whole number -- 30.
But what you can do is get the months and divide by 12. For greater precision, you could instead use days, or even smaller units.
LocalDate startDate = LocalDate.of(1984, Month.AUGUST, 10);
LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 10);
double numberOfMonths = ChronoUnit.MONTHS.between(startDate, endDate) / 12.0;
System.out.println(numberOfMonths); // prints 30.416666666666668
(If your endDate was February 10, 2015, then it prints 30.5....)

Getting hours,minutes, and seconds from Date? [duplicate]

This question already has answers here:
Java: getMinutes and getHours
(13 answers)
Closed 8 years ago.
I want to get the current hour,minute, and second from the Date object.
This is my code:
Date date = new Date();
int hour = date.getHours();
But I get a kind of error, the getHours() part doesnt work. I get a "The method getHours() from the type Data is deprecated". What does that mean, and how do I get the hour,minutes, and seconds?
Use Calendar:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hours = cal.get(Calendar.HOUR_OF_DAY);
From Date javadoc:
Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MINUTE).
Returns the number of minutes past the hour represented by this date, as interpreted in the local time zone. The value returned is between 0 and 59.

How to add or subtract 3 days from currentdate in java? [duplicate]

This question already has answers here:
How to add one day to a date? [duplicate]
(18 answers)
Closed 9 years ago.
I need to subtract 3 days from current date and need to store that in Date variable?
You could do this
Calender c = Calender.getInstance();
c.add(DAY_OF_MONTH,3);
c.add(DAY_OF_MONTH,-3);
Date d = c.getTime();
Pull the milliseconds-since-epoch value out, subtract three days of milliseconds from it, shove it into a new Date object.
public static final long ONE_DAY_MILLIS = 86400 * 1000;
Date now = new Date();
Date then = new Date(now.getTime() - (3 * ONE_DAY_MILLIS));

Categories

Resources