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);
Related
This question already has answers here:
Is it the last day of the month?
(4 answers)
Closed 11 months ago.
My input string date is as below:
String date = "1/31/2022";
I am transforming to a date:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
How do I know if my date is the last day of the month?
E.g.: for a String "1/31/2022" the output should be true. For 2/2/2023 it should be fast.
This is to run in Groovy in Jenkins but I believe I could use java as well
SimpleDateFormat is obsolete. I'd use the modern stuff from the java.time package.
Use single character M and d if not padding single-digit values with a leading zero.
DateTimeFormatter MY_FORMAT = DateTimeFormatter.ofPattern("M/d/uuuu");
LocalDate x = LocalDate.parse("1/31/2022", MY_FORMAT);
if (x.getDayOfMonth() == x.lengthOfMonth()) {
// it is the last day of the month
}
This question already has answers here:
How to compare dates in Java? [duplicate]
(11 answers)
Time comparison
(12 answers)
Closed 2 years ago.
I'm working on a Java program where the user, at some point, give us as an input two times (in format String). We can assume the times are from the same day. For example in the console:
Please introduce time 1:
16:00
Please introduce time 2:
10:00
My program will capture those two time values as a String and my idea was to parse it into an object of class Date.
How could I calculate which time goes first in the course of a day?
For the previous example, I would like to print the first time that takes place on the day.
How can I properly do the following?
if (date1 <= date2) System.out.println(date1); //Suppose date1 and date2 are Date objects
else System.out.println(date2);
String firstDateString = "16:00";
String secondDateString = "10:00";
LocalTime firstLocalTime = LocalTime.parse(firstDateString, DateTimeFormatter.ofPattern("HH:mm"));
LocalTime secondLocalTime = LocalTime.parse(secondDateString, DateTimeFormatter.ofPattern("HH:mm"));
if (firstLocalTime.isAfter(secondLocalTime)) {
System.out.println(secondLocalTime);
} else {
System.out.println(firstLocalTime);
}
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....)
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
This question already has answers here:
Generate random date of birth
(15 answers)
Closed 8 years ago.
I want to generate a random date as string in format - yy-MM-dd for the given year.
assume if i pass the year it should give a random date of the year.
I went through Random() as well as nextInt() but not sure how to get this
could you please help me on how to achieve this?
Thanks.
You can use a Calendar to do that:
final Calendar cal = Calendar.getInstance();
final Random rand = new Random();
final SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.DAY_OF_YEAR, rand.nextInt(cal.getActualMaximum(Calendar.DAY_OF_YEAR)) + 1);
System.out.println(format.format(cal.getTime()));
cal.getActualMaximum(Calendar.DAY_OF_YEAR) will return the number of the last day of the year the Calendar is set to (2014 in this code).
rand.nextInt() will return a number between 0 (inclusive) and the number of the last day (exclusive). You have to add 1 because the field Calendar.DAY_OF_YEAR starts at 1.
cal.set() will set the given field of the Calendar to the value given as the second argument. It is used here to set the year to 2014, and the day of the year to the random value.
SimpleDateFormat is used to print the date in the required format.
You can do with JSR-310 (Built in Java 8, but available to older versions)
public static LocalDate randomDateIn(int year) {
Year y = Year.of(year);
return y.atDay(1+new Random().nextInt(y.length()));
}
System.out.println(randomDateIn(2014));
prints something like
2014-05-21
If you want to work with random, this is how it works:
Random r=new Random();
int day=r.nextInt(30)+1; // (think of something yourself for implementing months with different amount of days)
int month=r.nextInt(11)+1; // (+1 because you don't want the zero)
However i see another answer, which if it works will probably save you a lot of time on implementing the days each month has.(and the leap year)