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 have a String stating the date as "2012-12-31" how can I convert it into date format using java and store it MySQL database where the type in MySQL is of date type.
You can do as follows
String string = "2012-12-31";
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date date = formatter.parse(string);
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); // convert java.util.date to java.sql.date
Have a look at SimpleDateFormat
String string = "2012-12-31"; //You have like this now
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
Date date = formatter.parse(string);
System.out.println(date);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
How to convert string to LocalDateTime,how to solve string format "yyyymmddhhMMss" to LocalDateTime
String dateTime = "20221120000000";
LocalDateTime localDateTime = LocalDateTime.parse(dateTime);
Exception in thread "main" java.time.format.DateTimeParseException: Text '20221120000000' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at java.time.LocalDateTime.parse(LocalDateTime.java:477)
at com.company.Main.main(Main.java:21)
This should do it:
String dateTimeString = "20221120000000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
If you dont want to use the DateTimeFormatter, your String needs to be in ISO format
Edit: it isn’t in the question, but you said elsewhere:
I want it to convert to this format("yyyy-MM-dd HH:mm:ss") …
A LocalDateTime cannot have a format (its toString method invariably produces an ISO 8601 format). So to obtain a specific format you need to convert to a String again:
DateTimeFormatter wantedFormatFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(wantedFormatFormatter);
System.out.println(formattedDateTime);
This outputs:
2022-11-20 00:00:00
(I hope you didn’t expect a LocalDateTime with the format you mentioned. In case you did see this question: Can’t rid of 'T' in LocalDateTime.)
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 5 years ago.
Improve this question
I want date without time in java
unix format is
YYYYMMDDHHMMSS.miliseconds
unix string 20170817134131.384
string val = "20170817134131.384";
if (val != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
try {
date = df.parse(String.valueOf(val));
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date: ", e);
}
return date;
}
Here's a simple solution to convert unix time to localDate and then you're free to format it as you please:
long time = 1491231860;
final LocalDate localDate = LocalDate.fromMillisSinceEpoch(unixSeconds);
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 5 years ago.
Improve this question
How do I parse this String in Java as date
"2017-06-12T14:45:00+05:30"
I've tried using SimpleDateFormat but it is throwing exception.
Thanks
It seems to be a valid ISO offset date time format, so this should work without requiring a formatter:
OffsetDateTime date = OffsetDateTime.parse(input);
If you really need a java.util.Date, you can then use:
Date legacyDate = Date.from(date.toInstant());
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH);
Date date = format.parse(string);
date is your solution
Just provide correct pattern in SimpleDateForamt
String startDateString = "09/23/2009";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to parse this String to timestamp timezone issue ?
String timestamp = "29-JAN-2014 01:00:00.000 PM EUROPE/PARIS";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS aa ZZZ");
Date dateFormatted = null;
try {
dateFormatted = df.parse(timestamp);
} catch (ParseException execption) {
execption.printStackTrace();
}
This seems to work:
TimeZone tz = TimeZone.getTimeZone("Europe/Paris")
String timestamp = "29-JAN-2014 01:00:00.000 PM";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS aa");
df.setTimeZone(tz)
try {
dateFormatted = df.parse(timestamp);
} catch (ParseException execption) {
execption.printStackTrace();
}
But you have to split the timezone from the timestamp
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
String s;
Format formatter;
Date date = new Date();
formatter = new SimpleDateFormat("yyMMdd");
java.util.Date date1 = ((DateFormat) formatter).parse(birthDate);
s = formatter.format(date1);
to format a date from yyyy-MM-dd to yyMMdd, but its not working. Please help.
Try this
String s;
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = formatter.parse("2013-07-17");
formatter = new SimpleDateFormat("yyMMdd");
s = formatter.format(date1);
System.out.println(s);