This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
how can i make Date data type from String in java?
input: String "2014/01/01 18:02:02" => output: Date 2014/01/01 18:02:02
You could use a DateFormat to parse and format (they're reciprocal functions) the String. Something like,
String str = "2014/01/01 18:02:02";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date d = df.parse(str);
System.out.println(df.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
Output is (as requested)
2014/01/01 18:02:02
please use SimpleDateFormat to parse String To Date.
In there you can find suitable DateFormat to convert this String to Date.
you can try this
String dateString = "2014/01/01 18:02:02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime StringAsDate = LocalDateTime.parse(dateString, formatter);
I would recommend using Time(java.time) API instead of java.util for your Date & Time needs as it is new and exclusively added to java for Date & Time operations.
You can use the following code snippet -
String dateString = "10-11-2014";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(dateString);
You can try this too.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String dateInString = "2014/01/01 18:02:02";
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
It is a working and it is also suitable to you as it is not complicated. you will get this output:
Wed Jan 01 18:02:02 IST 2014
2014/01/01 18:02:02
For your date following code should work :-
String myString = "2014/01/01 18:02:02";
DateFormat myDateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date myDate = myDateFormat.parse(myString);
For more details, see the documentation for SimpleDateFormat. Just make sure you use capital "MM" for month and small "mm" for minute.
Related
I'm trying to convert a string to date
I tried in couple ways but i'm getting the same.Please could any one help me out.
String stringDate ="20181228184821";
Date date = null;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
try {
date = sdf1.parse(stringDate);
System.out.println(date);
} catch (ParseException e) {
System.out.println("ICICIBusinessLogic Error in converting input date string ****" + e);
}
I get the following exception :
Error in converting input date string ****java.text.ParseException:
Unparseable date: "20181228184821"
Your stringDate should exactly match your format.
20181228184821 => 20181228 18:48:21 should work.
Or change your format to yyyyMMddHHmmss
You should use DateTimeFormatter and the new classes in java.time instead of the old SimpleDateFormat api. That being said, you appear to have confused your input and your output formats.
String fmtIn = "yyyyMMddHHmmss", fmtOut = "yyyyMMdd HH:mm:ss";
String stringDate = "20181228184821";
TemporalAccessor dt = DateTimeFormatter.ofPattern(fmtIn).parse(stringDate);
System.out.println(LocalDateTime.from(dt));
System.out.println(DateTimeFormatter.ofPattern(fmtOut).format(dt));
You are using the wrong pattern, use the following instead :
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
Also, SimpleDateFormat is long outdated, make use of java-8's java.time
LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
The pattern specified in Simple Date Format Constructor should be same as the string to be parsed.
Try
String stringDate ="20181228 18:48:21";
I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I was trying to convert date string 08-12-2017 to 2017-12-08(LocalDate). Here is what I tried-
String startDateString = "08-12-2017";
LocalDate date = LocalDate.parse(startDateString);
System.out.println(date);
Also tried using formatter, but getting same result, an DateTimeParseException.
How can I get an output like 2017-12-08, without getting an exception?
Try this (see update below)
try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}
Update - Java 8
String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));
First you have to parse the string representation of your date-time into a Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");
Then you format the Date object back into a String in your preferred format.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 6 years ago.
I have date in string object. I want to convert into Date object.
Date getDateFmString(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(dateString);
return convertedCurrentDate ;
}
above function returning following output.
Fri Apr 22 00:00:00 IST 2016
but I want output in this format '2016-03-01' only
function should take string only.
function should return Date object.
I have done lot of research over web, but I got solution from one Expert.
Date getDateFrmString(String dDate)
{
java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
return dDate;
}
this is what I want.
Change the date format from
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
to
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Hope this works
See this example
public Class DateFormatDemo{
public static void main (String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";
try{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch(ParseException e){
e.printStackTrace();
}
}
}
This link might help you with string to date object conversions
You are parsing with the wrong format try
String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Output:
2016-01-01
DEMO1
And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.
String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
OUTPUT:
01-01-2016
DEMO2
To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.
Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".
Let's do it step by step:
You have a date as String in dd-MM-yyyy format.
You want to convert it into date. (for this you are using SimpleDateFormat)
Now you are printing the date. Question here is are you printing the converted date object or input string?
If its a date object then toString method is called of date class.
As per comment on java.util.Date class it's:
dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016
So that coincides with what you get in output in the second approach. But how is that code even running is strange.
String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);
Variable 'input' is not defined.
What are the possible solutions:
While printing date, convert it back to String using SimpleDateFormat as per the requirement.
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dStr = sdf.format(dateString);
System.out.printn(dStr);
Extending class java.util.Date and override toString, but that would be a bad idea.
This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 9 years ago.
I have a string and i need to convert it to date in the "YYYY-mm-dd" format which i'm unable to do. I checked the similar post on stckoverflow but didnt help so i'm posting this query.
String stringDate = "Dec 13, 2013";
I need this to be convert to
Date reportDate = 2013-12-13";
when i use simpledateformat i got unparsable date. Please help
Check this out:
try {
SimpleDateFormat format1 = new SimpleDateFormat("MMM dd, yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
String stringDate = "Dec 13, 2013";
Date date = format1.parse(stringDate);
System.out.println(format2.format(date));
} catch (ParseException exp) {
// Take appropriate action
}
Output: 2013-12-13
In programming a very important skill is taking information that is close to what you need and using it to find what you do need.
Look at the link in the comments and you can see:
http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
Read that and you have what you need to adjust the answer there to give the results you want.
Use SimpleDateFormat as here:
new_date is your Date
String stringDate = "Dec 13, 2013";
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
Date new_date=format.parse(stringDate);
String date="Your date";
SimpleDateFormat format = new SimpleDateFormat("MMM DD, YYYY");
Date d= format.parse(date);
DateFormat newFormat = new SimpleDateFormat("YYYY-MM-DD");
System.out.println(newFormat.format(d));
I would do something like:
SimpleDateFormat parser = new SimpleDateFormat("MMM dd, yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date reportDate = parser.parse(stringDate)
String result = formatter.format(reportDate);