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
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 needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying gmt asia/kolkata timemillis convert to utc timemillis but it returns same value. Environment time is asia/kolkata
The epoch is time zone independent. So you should get the same number of milliseconds since the epoch back no matter which time zone you convert to.
So the result you got is correct.
You can try as this
Instant date = Instant.ofEpochMilli(1549362600000l);
LocalDateTime utc = LocalDateTime.ofInstant(date, ZoneOffset.UTC);
You need not to use Joda time api has moved to java 1.8 has implemented the same you can use above same from java.time package
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How can I convert the current time in milliseconds, which is a Long, to a date in specific format?
The format that I need is yyyy-MM-dd HH:mm. This should be of type Date, not String.
You are confused. The type Date is a number of milliseconds since January 1 1970 midnight UTC. It has no inherent format. There is a default system format for a Date, but you cannot alter it. You will need to format your Date as a String if you need that particular String format.
This question already has answers here:
Java Date - Insert into database
(10 answers)
Closed 6 years ago.
How can I best store a String with format yyyy-MM-dd (without time declaration) in SQL database (postgres)?
I later want to use that String always as Date type. I also want to execute query against the database to give me records that are before or after that Date.
Should I store it as a String or as a Date type in DB?
If I store it as a Date, in database I see yyyy-MM-dd HH:mm:ss. How could I prevent the time declaration?
If you do not want to store a time component, then use the DATE data type. It does not have a time or a time zone component, so is useful for dates of birth, dates of employment start/end, and other data for which the time is not relevant.
http://www.postgresql.org/docs/9.3/static/datatype-datetime.html
The display format is a matter for the application -- just use the correct data type. YYYY-MM-DD is documented as the best format for suplying dates, though.
Always recommended one is Date with time-stamp. If you don't need then while storing store it as 00:00:00.(Use Sql Date for date without time-stamp.)
Use business logical in order to truncate the time and the format you required. Service layer you can play with date and in most of DB its better to store Date with timestamp.
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
To my knowledge, System.currentTimeMillis()/1000 can show the current time in seconds since
1970-1-1 00:00:00 (YY-MM-DD HH:mm:ss)
For example
2013-10-12 21:30:00 (YY-MM-DD HH:mm:ss)
= 13815846XX (not sure whats X for)
I was wondering how to calculate it. Thanks a lot!!!!
System.currentTimeMillis() just returns the number of milliseconds since the Unix epoch (January 1st 1970, midnight UTC), as a long.
Converting that value into a string is normally the job of something like SimpleDateFormat, via Calendar and Date. Alternatively, look at Joda Time for a nicer date/time API.
If you want to start with a date and get the number of milliseconds since the Unix epoch, you'd use Calendar, set the appropriate fields and then use Calendar.getTimeInMillis(). (Or again, use Joda Time.) Be careful about time zone interactions.
You can use Epoch Converter to check your computations.
A value such as 1381584600 is most likely to be a Unix timestamp, which is the number of seconds (not milliseconds) since the Unix epoch - hence the division by 1000 that you mention.
If this doesn't tell you what you need, please ask a more precise question.