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);
Related
I have a string containing date with timezone like
2014-01-24 09.05.14 -06:00.
I need to convert this into date format as YYYYMMDD.
What is the best way in Java to convert the above string into date.
Do it like this (requires JDK 7+):
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss X");
DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = "2014-01-24 09.05.14 -06:00";
Date date = inputFormat.parse(dateString);
System.out.println(outputFormat.format(date));
One way of doing it is to convert the String into a Date and then use a DateFormatter (e.g. the SimpleDateFormat) to formate that date.
Take a look at this Tutorial for further information.
I have a date (05/15/2013) which is from a HTML Datepicker. I want to save this value in a mySQL column, which is the type of DATETIME. Format should be yyyy-MM-dd.
you can use this
STR_TO_DATE
STR_TO_DATE(string, '%d/%m/%Y')
You can specify the format as per your requirement
You could use joda time with date formatters like this:
DateTime dateTime = DateTime.parse("05/15/2013", DateTimeFormat.forPattern("mm/dd/yyyy"));
String result = dateTime.toString(DateTimeFormat.forPattern("yyyy-mm-dd"));
You can use java.text.SimpleDateFormat class, e.g.
String pattern = "dd/MM/yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date today = new Date();
String output = formatter.format(today);
You can find more on official Oracle tutorial page.
You can try this
String new_date_string = your_date_string.replace("/", "-");
System.out.println(str);
or you can use regex
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(new_date_string);
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
dt = formatter.parse(temp[0]);
gives me the error
java.text.ParseException: Unparseable date: "30-MAR-07"
Is there any way that I can format the given String to a Date object without having to write my own string splitting and translating-to-months-in-numerals methods? Thanks
Use correct format, as follows,
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Your SimpleDateFormat pattern is completely incorrect for "30-MAR-07". It's got the values in the wrong order, it uses the wrong separator, and the wrong month format. You want:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
You may also want to specify the locale, e.g.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);
I've checked that this works - at least on my machine :)
Try this... (new SimpleDateFormat("dd-MMM-yy")
System.out.println(new SimpleDateFormat("dd-MMM-yy").format(new Date("30-MAR-07")));
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
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')