This question already has answers here:
How to convert LocalDate to SQL Date Java?
(3 answers)
Closed 7 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
What's the correct way to convert between java.sql.Date and LocalDate (in both directions) in Java 8 (or higher)?
The Java 8 version (and later) of java.sql.Date has built in support for LocalDate, including toLocalDate and valueOf(LocalDate).
To convert from LocalDate to java.sql.Date you can use
java.sql.Date.valueOf( localDate );
And to convert from java.sql.Date to LocalDate:
sqlDate.toLocalDate();
Time zones:
The LocalDate type stores no time zone information, while java.sql.Date does. Therefore, when using the above conversions, the results depend on the system's default timezone (as pointed out in the comments).
If you don't want to rely on the default timezone, you can use the following conversion:
Date now = new Date();
LocalDate current = now.toInstant()
.atZone(ZoneId.systemDefault()) // Specify the correct timezone
.toLocalDate();
Related
This question already has answers here:
String to Date Conversion mm/dd/yy to YYYY-MM-DD in java [duplicate]
(5 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 4 years ago.
I got some values
"createTime": 1527217399000,
"updateTime": 1527218049000,
"createTime": 1527217399000,
"updateTime": 1527217954000,
But I can not parse them to date format, I'm not sure if there is something wrong with the data format,is there any method to parse them?
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Long tempTime = 1527217399000L;
Date createDate=format.parse(tempTime.toString());
System.out.println(createDate.toString());
Exception in thread "main" java.text.ParseException: Unparseable date:
"1527217399000"
at java.text.DateFormat.parse(DateFormat.java:366)
If you want to create a Date instance from a long value, use:
Date createDate=new Date(tempTime);
As the Javadoc mentions:
java.util.Date.Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) is meant to parse a String of the specified format into a Date (i.e. if you wanted to parse a String such as "2018-05-28 06:12:00" into a Date instance).
Since it's 2018, you really should be making use of the java.time API introduced in Java 8
long createTime = 1527217399000L;
LocalDateTime ldt = Instant.ofEpochMilli(createTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
String format = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH));
System.out.println(format);
Which prints 2018-05-25 13:03:19 (on PC)
And if you don't have Java 8, there's the ThreeTen backport which makes the API available to earlier versions of Java
The old java.util.Date API is notoriously poor and if you want to make clear decisions about what type of date / time you're using - whether it's in a particular timezone, etc. - you should use the java.time API.
Assuming (you'd need to confirm) that these millisecond timestamps are in UTC timezone, you can use code like this to format it as a date/time in your local system timezone.
Long tempTime = 1527217399000L;
Instant instant = Instant.ofEpochMilli(tempTime);
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
This question already has answers here:
Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java [duplicate]
(2 answers)
Closed 4 years ago.
I need to transform a Twitter timestampe into a Java Date object,
here is an example of a value of a Timestampe: "2015-01-06T21:07:00Z"
Can you please give me sample of java code (standard Java) doing the job?
Thank you
I recommend you take advantage of the new Date/Time API introduced in Java 8, specifically Instant as follows:
Instant.parse("2015-01-06T21:07:00Z");
You can then perform a multitude of operations, but keep in mind that the object is immutable, so any changes to the instance (that aren't chained) must be stored in a separate variable.
Actually it is ISO 8601 format for UTC time zone.
It conforms with XML DateTime format as well.
So, to get actual java.util.Calendar or java.util.Date out of it you simply can use available in JDK
Calendar twitterCalendar = javax.xml.bind.DatatypeConverter.parseDateTime("2015-01-06T21:07:00Z");
Date twitterDate = javax.xml.bind.DatatypeConverter.parseDateTime("2015-01-06T21:07:00Z").getTime();
Just be aware: java.util.Date has no Time Zone information in it. Your string is in UTC, so if you try to print value of twitterDate you will see Date/Time in TimeZone of your computer/server. Still actual value of twitterDate stays the same
millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
How to covert date variable to java.sql.date
(1 answer)
Closed 5 years ago.
I have the following code snippet where i get continous error for casting java.util.date to java.sql.date.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
storedProcedureCall.setLong(1, 12345);
storedProcedureCall.setDate(2, (java.sql.Date) sdf.parse("09/02/2017"));
storedProcedureCall.setDate(3, (java.sql.Date) sdf.parse("10/02/2017"));
What am i doing wrong here. I have imported the java.util.Date package as well.
The class java.util.Date is totally different to java.sql.Date. Thus it can not be cast to.
You probably meant to use java.util.Date. Check your signature of storedProcedureCall#setDate, it probably accidentally imported java.sql.Date instead of java.util.Date.
Note that nowadays the Date class should not be used anymore. Instead use the new modern API located inside the package java.time. Like this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime dateTime = LocalDateTime.parse(dateAsString, formatter);
This question already has answers here:
SimpleDateFormat parsing date with 'Z' literal [duplicate]
(12 answers)
Closed 6 years ago.
I wonder which format is the following datetime value:
"2016-05-18T12:05:33Z"
This date time format is used on Zendesk's tickets in the fields of created_at and updated_at.
I know that its "yyyy-MM-ddTHH:mm:ss........", but what does the "Z" stand for?
What I want to do is parse and convert into a java.time class for storing dates and times, but I do not know which is the best one.
That is ISO 8601 format and the Z is the timezone indicator; it means UTC.
The best java.time class to use is ZonedDateTime. Example:
ZonedDateTime dateTime = ZonedDateTime.parse("2016-05-18T12:05:33Z",
DateTimeFormatter.ISO_DATE_TIME);
This question already has answers here:
Convert LocalDate to LocalDateTime or java.sql.Timestamp
(7 answers)
Closed 4 years ago.
How do you convert a Localdatetime to timestamp? I want to use the new SE 8 date api because it is better than the util date and calendar. I plan to use localdatetime throughout my program and then place that date into a mysql database. I have looked for an answer but there doesn't seem to be very many questions and answers for java.time. This is a little of the code that I am testing. This is as far as I got.
LocalDateTime c = LocalDateTime.now();
java.sql.Timestamp javaSqlDate = new java.sql.Timestamp(c.getLong());
I think I need to convert it into a long first, but I don't know how. The api allows for converting individual elements like month and day, but not for the whole date. Since I'm already here, how do you convert back from timestamp? Should I just use jodatime?
I tried this:
LocalDateTime c = LocalDateTime.now();
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("this:" + c);
java.sql.Timestamp javaSqlDate = new java.sql.Timestamp(c.atZone(zoneId).toEpochSecond());
pst.setTimestamp(2, javaSqlDate);
This only saves the date around 1970. The system.print prints the current date correctly. I want it to save the current date.
LocalDateTime l = LocalDateTime.now();
Timestamp t = Timestamp.valueOf(l);
Source: https://coderanch.com/t/651936/databases/Convert-java-time-LocalDateTime-SE
First of all, you should decide if you really want to use LocalDateTime.
Below are some explanations about the difference, taken from here:
<...> LocalDateTime is not a point on the time line as Instant is, LocalDateTime is just a date and time as a person would write on a note. Consider the following example: two persons which were born at 11am, July the 2nd 2013. The first was born in the UK while the second in California. If we ask any of them for their birth date it will look that they were born on the same time (this is the LocalDateTime) but if we align the dates on the timeline (using Instant) we will find out that the one born in California is few hours younger than the one born in the UK (NB: to create the appropriate Instant we have to convert the time to UTC, this is where the difference lays).<...>
In order to get long from Instant you could use getEpochSecond() method.
In order to get long from LocalDateTime you should provide a timezone.