get date difference in java, use of getDate() [duplicate] - java

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
getting the difference between date in days in java [duplicate]
(3 answers)
Closed 10 years ago.
I am co-working with a group, I want to ask how can i get the date difference from a formate of "Sat Feb 23 00:00:00 GMT 2013". The pickerfrom and to is a calendar, and getDate returns that formate. How can I get the date difference in days? any idea?
/* Current format Sat Feb 23 00:00:00 GMT 2013 */
Date date_from = pickerFrom.getDate();
Date date_to = pickerTo.getDate();
int date_diff = (int)((date_to)-(date_from));

Checkout getting the difference between date in days in java
My preference would be to use Joda time - it has many useful date functions that'll make your life much easier when it comes to dates and date manipulation

You can get the difference in milliseconds of each date and subtract these values.
long diff = date_to.getTime() - date_from.getTime();
will return you the number of milliseconds between the two dates.
Then, you can use something like this to get the number of hours, days,... out of these milliseconds.

Related

How Covert unknown date format to UTC in Java [duplicate]

This question already has answers here:
how to convert milliseconds to date format in android?
(17 answers)
Closed 3 years ago.
I have to convert the following time to UTC but I don't know what format the current time is in. How do I convert it to UTC using Java?
Unknown format:
1561554154352
It clear to me which or how to do it based on the Java Date documentation.
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
That isn't so much a Date as it is an offset to an epoch (specifically, the number of milliseconds since midnight January 1, 1970 UTC). To convert it to a date, pass that number to the date constructor. Like,
long epochTime = 1561554154352L;
System.out.println(new Date(epochTime));
Outputs
Wed Jun 26 09:02:34 EDT 2019
The following will get you from Zulu(UTC):
System.out.println("Current elapsed from epoch => " + System.currentTimeMillis());
System.out.println("Convereted to UTC =>" + Instant.ofEpochMilli(System.currentTimeMillis()).toString());

Getting 24hr time when adding miliseconds to a Date? [duplicate]

This question already has answers here:
How to add one day to a date? [duplicate]
(18 answers)
Closed 5 years ago.
I have the following Java code that takes a date and should add a full day to the date:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = "2017-01-30T19:00:00+0000"
Date date = formatter.parse(dateString);
long timeBetweenStartDates = 24 * 60 * 1000;
Long DateWithOneDayAddedInMilis = date.getTime()+timeBetweenStartDates;
Date dateWithOneDayAdded = new Date((DateWithOneDayAddedInMilis));
The value I am getting for dateWithOneDayAdded is:
Mon Jan 30 13:24:00 GMT 2017
What I am looking for here would be:
Tue Jan 31 13:24:00 GMT 2017
How can I ensure that the date is in the format I expect?
Oh, what a wonderful example of where the newer date and time classes are much more programmer-friendly. With these it’s next to impossible to make an error like the one you made (and which is pretty easy to make with the old classes, and in particular, very hard to spot once you have written the code).
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = "2017-01-30T19:00:00+0000";
OffsetDateTime dateTime = OffsetDateTime.parse(dateString, formatter);
OffsetDateTime dateWithOneDayAdded = dateTime.plusDays(1);
System.out.println(dateWithOneDayAdded);
This prints
2017-01-31T19:00Z
You may of course format the calculated date-time the way you or your users prefer.
An added benefit is that plusDays() handles transistion to and from summer time (DST) nicely and hits the same time on the next day (if possible) rather than blindly adding 24 hours.

How to convert current UTC time into Linux Timestamp [duplicate]

This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Android Get Current timestamp?
(14 answers)
Closed 6 years ago.
I am trying to get current android device time, and convert it into UTC timezone, then i need to convert it into Unix Timestamp.
I google it, found some solutions, tried few, but nothing helping me here.
This is what i am doing now.
Date date;
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
date= dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
date.getTime();
Output:
date(Its returning the correct UTC date time) Thu Jan 26 08:06:20 GMT+05:00 2017
date.getTime() returns 1485399980000
When i put this Timestamp in online tools, Its not returning right output.
Kindly guide me how to convert current UTC time into UnixTimestamp
What you need is much simpler:
new Date().getTime()
It is alread in UTC. To get a Linux timestamp you have to divide this by 1000.

Convert String to Date not working properly [duplicate]

This question already has answers here:
java date problem in parsing
(4 answers)
Closed 7 years ago.
I am getting a date by ajax in String format. But it is getting changed when I am converting it to date by SimpleDateFormat. The month is always changed to Jan. I am worried only about the month change.My code is given below
String appointmentDate = request.getParameter("appointmentDate");
System.out.println(" appointment date in String format "+appointmentDate);
Here I am getting the date correctly(16/12/2015). But when I am changing it to Date format it is getting changed(Fri Jan 16 00:12:00 IST 2015). Whatever I input the month, say August, May, June, I am always getting month Jan.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
Date parsedDate = dateFormat.parse(appointmentDate);
System.out.println(" appointment date in DATE format "+parsedDate);
Please help me out. Thanks in advance.
As per the JavaDoc, lower case m denotes minutes, not months.
Changing your expression to dd/MM/yyyy should fix the issue.

Handling non understandable behaviour of Java date formats. [duplicate]

This question already has answers here:
How to convert a date String to a Date or Calendar object?
(5 answers)
Closed 6 years ago.
As I am not expert in handling of dates in java but I am unable to understand this behaviour.Here is my code
Date from = new SimpleDateFormat("dd/MM/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output
Sat Jul 05 00:07:00 PKT 2013
And this is 2nd another code snippet
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output:
Sat Jan 05 00:07:00 PKT 2013
Now the thing which is considerable is format. This format dd/MM/yyyy which have MM gave me correct output but this format dd/mm/yyyy which have small mm gave me wrong output (always give jan in month).I read the doc where it is mentioned that samll m is for minutes and capital M is for month My question is Can I never use small m here? if no , then why it is giving the result and on which basis it is giving jan everytime I know this is a basic question but after searching and after not finding any understandable thing , I posted it.Thanks
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
that mm in your format is for minutes. MM is for month.
Those formatting placeholders are fixed. small m is always for minutes. And it's January because this is the default Month value.
mm is for minutes so you do not have any month in your date. Thus, I guess that the month is initialized to 0 (Jan)
The reason it does not fail is because by default the formatter is lenient. If you want it to fail then setLenient(false) on the formatter object.
Although I do not think it will fail in your case as in your example it will read 07 as minutes.
When using
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
the simple mm is for minutes therefore i think the value for month is assumed to be 0 so it gives Jan as the default value so use MM for month.
I guess it comes from calendar.clear() which will point to 1 Jan 1970. Then it adds you parsed data = 2013 years (YY), 5 days (dd) and 7 minutes (mm). Use MM for month

Categories