Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have two resultset each having only one column. The column field of both resultset contain a time entry in a format like this (2012-12-31 13:49:21.999). Now can anyone help me to find difference of time between two columns of the resultset?
e.g. if first field of column of first resultset has entry (2013-02-13 17:04:09.672) and first field of column of second resultset has entry (2012-12-31 13:49:21.999) then the program should be able to difference in the time of these two enries.
need help?
Something like:
Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH).parse(column1);
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH).parse(column2);
getDateDiff(date1,date2,TimeUnit.MINUTES);
/**
* Get a diff between two dates
* #param date1 the oldest date
* #param date2 the newest date
* #param timeUnit the unit in which you want the diff
* #return the diff value, in the provided unit
*/
public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
long diffInMillies = date2.getTime() - date1.getTime();
return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on Selenium Java, I need to get the following date format without the time, as a string in selenium java to validate whether it is up to date with the published date. I used getText() method from the website by splitting from the time and date. Is there any other best ways rather than this solution!
java.time
Edit: I have added more explanation and more code lines.
There’s a little challenge in the fact that the string on the website does not include year. One simple way to handle it is:
ZoneId websiteTimeZone = ZoneId.of("America/Lower_Princes");
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("dd-MMM HH:mm", Locale.ENGLISH);
String stringFromWebsite = "06-Feb 06:37";
MonthDay today = MonthDay.now(websiteTimeZone);
System.out.println("Today is " + today);
MonthDay date = MonthDay.parse(stringFromWebsite, formatter);
System.out.println("Date from website is " + date);
if (date.equals(today)) {
System.out.println("It’s up to date");
} else {
System.out.println("It’s *NOT* up to date");
}
When I ran today (March 12), the snippet printed:
Today is --03-12
Date from website is --02-06
It’s *NOT* up to date
A MonthDay is a month and day of month without year. The advantage of using this class is we don’t need concern ourselves with year. A possible drawback is we can’t compare two such objects determine which one is before or after the other one. Such a comparison would require knowing the year of each one.
We need to know the time zone that the website uses since it is never the same date everywhere on Earth. Please insert the correct one where I put America/Lower_Princes.
I am parsing the string from the website into a MonthDay using a DateTimeFormatter with format pattern dd-MMM HH:mm since lower case d is for day of month, M is for month, H for hour of day and lower case m for minut of the hour. Since I am parsing into a MonthDay, the time is ignored (only its syntax still checked). In the print --03-12 means March 12 and --02-06 similarly February 6 (the date from the website). Since they are not the same, the code prints that the website is not up to date.
A more advanced solution might check if the date is a few days before or after today’s date and/or also look at the time.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Stack Overflow question How do I simply parse a date without a year specified?
You can use selenium's getText(), in order to acquire the value as a String.
Afterwards you can use Java's DateTimeFormatter, to parse this date, and transform it to the format you want
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to parse timestamps using the format "yyyymmddHHssmm".
I have two such time stamps:
String timeStamp1 = "20190612221303"//this means 12June2019 10:13:03pm
String timeStamp2 = "20190512222303"//this means 12May2019 10:23:03pm
So I am trying to convert these timestamp string to java date using the following :
Date date1= new SimpleDateFormat("yyyymmddHHssmm").parse(timeStamp1);
Date date2 = new SimpleDateFormat("yyyymmddHHssmm").parse(timeStamp2);
So obviously when I do a
System.out.println(date1.getTime() > date2.getTime());
I would expect the above statement to print true.
But alas it prints false.
Inface the .getTime() of Date prints 1547310793000 for date1 and 1547310803000 for date2, which is obviously incorrect.
Could someone point out what is going on here.
In the format string, you have mm twice: yyyymmddHHssmm. The first occurrence should be MM, for month of year.
What is happening is that you are using
m Minute in hour
And your TimeStamp it is parsing with date
Sat Jan 12 22:03:13 Date1
Sat Jan 12 22:03:23 Date2
You need to use
M Month in year
Check more in the documentation
The format that you have used:yyyymmddHHssmm is ambiguous.
I believe the 5th and 6th characters are used to define months.
Use MM in caps for that.
You have used small mm, which means minutes
Your String passed to SimpleDateFormat should be yyyyMMddHHmmss . Take look here which letter stands for which thing in that formatter. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can I compare same date of births in GWT using Date?
olpPatient.getBirthday().equals(birthday);
Even if both dates are equal, that line returns false.
You can use the class com.google.gwt.user.datepicker.client.CalendarUtil. There is a method called isSameDate(Date, Date) which will just check the date, not the time of day.
From the javadoc:
Check if two dates represent the same date of the same year, even if they have different times.
It depends on how you create dates. If you get them from a DatePicker or parse a String which does not include hours and minutes, then you can use:
patient.getBirthday().getTime() == birthday.getTime();
If your dates include hours and minutes, you can do:
int day = 24 * 60 * 60 * 1000;
boolean sameDate = patient.getBirthday().getTime()/day == birthday.getTime()/day;
You are comparing Date objects by using equals() (by default, Object.equals is checking whether references to these objects are equal or not and Date.equals checking timestamps). But you need only to exract day, month, year from those objects and compare only them.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i want to have FirstDay of week and last Day of week into the Date Format means i need whole Week date in java using DateChooser means i need to have that whole Existing week for that date. show how to get it i trying to have that logic.
Please Help me and thank you in advance for your Response..
You can use the Calendar class.
Calendar cal = Calendar.getInstance ();
int dayofWeek = cal.get (Calendar.DAY_OF_WEEK);
cal.add (Calendar.DATE, -1 * (dayofWeek - Calendar.MONDAY));
Date fdow = cal.getTime ();
As the cal object is now modified, just add six to get the last day of the week.
cal.add (Calendar.DATE, 6);
Date ldow = cal.getTime ();
Using the third-party Joda-Time makes it a bit easier.
See the question Get first day of a particular week in Joda-Time? java.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
is the java.sql.Date() and the mysql command sysdate gives exactly the same date time?
You will run the command sysdate on the DB system . It gives you the system date of the DB server.
java.sql.Date is A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. It is different , it is a Java object which interfaces with the sql date. The JDBC driver will use the java.sql.Date object and format its data to an acceptable format while storing it in the DB and vice versa.
By itself it cannot give us a date until you feed it with some data, and it stores years, months and days while hour, minute, second and millisecond are ignored.
The SYSDATE function gives the date and time of the database server. The java.sql.Date does not give any particular date - it must be initialized to a specific date, and it only stores the date, not the time. (Internally it does store a time component as well, but it's meaningless and you're supposed to ignore it.) If you create a java.sql.Date object with
date = new java.sql.Date(System.getCurrentTimeMillis());
or something equivalent and your database is running on a different machine, you may get a different date when you call SYSDATE if the clocks on the two machines are not synchronized.
SELECT SYSDATE();
Output: 2013/07/26 04:16:55
Default Format: yyyy-MM-dd hh:mm:ss
Date a=new Date();
java.sql.Date d=new java.sql.Date(a.getTime());
System.out.println(d);
Output: 2013/07/26
Default Format: yyyy-MM-dd