Java String to Date Convertion - java

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";

Related

Java how to make date from string [duplicate]

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.

How to convert a string to date format in java?

How to convert a String to DateFormat in java?
I am writing a application where I want to convert a string "20050105000200" to "2005-01-05 00:02:00". Is there a direct way to do it in Java? I want both input and output in String. Please let me know if there is a way to do it directly.
Can you give me some simple codes?
Thanks.
You can use SimpleDateFormat to parse the input date, and then again to format the output
SimpleDateFormat inFmt = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = inFmt.parse("20050105000200");
System.out.println(outFmt.format(d));
You should first parse it to a date like this:
http://www.exampledepot.com/egs/java.text/parsedate.html
and then format it again like this:
http://www.exampledepot.com/egs/java.text/formatdate.html
This example might help you
String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
Use a DateFormat to parse() this String to a Date object. Use a different DateFormat to format() the Date to the String representation you want.
See this
You can use simpledateformat class for doing that
Use SimpleDateFormat. Haven't tried on my IDE, but it goes something like this:
SimpleDateFormat fromUser = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String reformattedStr = myFormat.format(fromUser.parse("20050105000200"));
System.out.println(reformattedStr);

How can I convert this string to a standard date in java?

Earlier I posted the following question: How can I convert this date in Java?
But now I would like to know how I can convert this string into a date/time.
2010-03-15T16:34:46Z
For example: 03/15/10
UPDATED:
String pattern = "MM/dd/yy 'at' HH:mm";
Date date = new Date();
try {
date = new SimpleDateFormat(pattern).parse(q.getUpdated_at());
} catch (ParseException e) {
e.printStackTrace();
}
dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));
Gives me a result like:
Mon Mar 15 16:34:50 MST 2010
How can I format it to be
03/15/10 at 4:34PM
?
Both SimpleDateFormat and joda-time DateTimeFormat can parse this, using this pattern:
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
For example:
Date date = new SimpleDateFormat(pattern).parse(dateString);
And (joda-time):
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(s);
Update
You have 2 date formats involved - one for parsing the input, and one for formatting the output. So:
dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));
(Of course, for the sake of optimization, you can instantiate the SimpleDateFormat only once, and reuse it)
In a nutshell, you want to convert a date in a string format to a date in another string format. You have:
2010-03-15T16:34:46Z
and you want
03/15/10 at 4:34PM
You don't want to end up using java.util.Date object as you initially implied in your question. You also don't want to use its toString() since that returns a fixed format as definied in its javadoc.
The answer of Bozho still applies. Use java.text.SimpleDateFormat. First, you need to parse the date in string format into a Date object so that you can format it back into another string format.
// First parse string in pattern "yyyy-MM-dd'T'HH:mm:ss'Z'" to date object.
String dateString1 = "2010-03-15T16:34:46Z";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(dateString1);
// Then format date object to string in pattern "MM/dd/yy 'at' h:mma".
String dateString2 = new SimpleDateFormat("MM/dd/yy 'at' h:mma").format(date);
System.out.println(dateString2); // 03/15/10 at 4:34PM
If you want to format output string, change following line in your code
dateText.setText(date.toString());
to
dateText.setText(String.format("%1$tm/%1$td/%1$ty at %1$tl:%1$tM%1$Tp", date));

String to Date in Java

HI,
I am converting String to Date format. But it returns wrong dates. for example,
String startDate = "08-05-2010"; // (MM/dd/yyyy)
I want to convert this to "Date" object like this, 05-JUL-10
How to do that? I tried like this
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
scal1.setTime(dateFormat.parse((startDate)));
but i am getting "Unparseable date:" .
If you want to convert a date string of one format to another format, you can use format() and parse() methods of SimpleDateFormat class
First you need to parse the string to a date object using parse() method setting the source pattern and then format the date object using format() method setting the target pattern:
SimpleDateFormat sourceFormat = new SimpleDateFormat("MM-dd-yyyy");
Date sourceFormatDate = sourceFormat.parse("08-05-2010");
SimpleDateFormat destFormat = new SimpleDateFormat("dd-MMM-yy");
String destFormatDateString = destFormat.format(sourceFormatDate);
System.out.println(destFormatDateString); // 05-Aug-10
Unless you've left something out, it looks like you're trying to parse it with the wrong format, i.e. you have an mm-dd-yyyy, and you're trying to parse it with the format dd-MMM-yy. Try a separate date format for what you're parsing from what you're encoding.
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);
String strDate = “2007-12-25″;
Date date = null;
try {
date = format.parse(strDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
The format dd-MMM-yy you use to parse the string is wrong; the format should be dd-MM-yyyy.
String startDate = "08-05-2010";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = dateFormat.parse((startDate));
Note that Date objects themselves do not have a format: a Date objects just represents a date and time value, just like an int is just a number, and doesn't have any inherent formatting information. If you want to display the Date in a certain format, you'll have to use a DateFormat object again to format it:
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
System.out.println(dateFormat.format(date));
// output: 08-May-10

Date conversion in Java

How can I take a string in a format such as: 2008-06-02 00:00:00.0 and convert it to: 02-Jun-2008?
Can I somehow take the original string, convert it to a Date object, then use a formatter to get the final output (rather than parsing the string myself)? Thanks!
You can use SimpleDateFormat to convert between a String and a Date object and vice versa based on a pattern. Click the API link, you'll see patterns being explained in detail. A 4-digit year can be represented with yyyy, a 3-character month abbreviation can be represented with MMM and so on.
First you need to parse the String of the first format into a Date object:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf1.parse(inputString);
Then you need to format the Date into a String of the second format:
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String outputString = sdf2.format(date);
Note that you need to take the Locale into account as well to get the month to be printed in English, else it will use the platform's default locale to translate the month.
Use 2 instances of SimpleDateFormat class. One for converting your input string to date and second to convert date back to string but in another format.
Here is an example of using SimpleDateFormat.
DateFormat startFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat endFormat = new SimpleDateFormat("dd-MM-yyyy");
String outputString = null;
try {
Date date = startFormat.parse(inputString);
outputString = endFormat.format(date);
} catch(ParseException pe) {
throw new IllegalArgumentException(inputString + " is not properly formated.", pe);
}
You can definitely use SimpleDateFormat class like others have recommended.
Another suggestion if it applies in your case. If you are getting this data from a sql query you can also use to_char() method to format it in the query itself. For example: to_char(column_name,'DD-MON-YYYY')

Categories