I'm trying to parse a String into a Date and then format that Date into a different String format for outputting.
My date formatting code is as follows:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat dateParser = new SimpleDateFormat("d/M/yyyy h:m:s a");
String formattedDocumentDate = dateFormatter.format(dateParser.parse(sysObj.getString("document_date")));
The result of sysObj.getString("document_date") is 1/31/2013 12:00:01 AM. And when I check the value of formattedDocumentDate I get 01/07/2015.
Any help is much appreciated.
You are parsing days 31 as months. SimpleDateFormat tries to give you a valid date. Therefore It adds to 1/0/2013 31 months. This is 2 years and 7 month. So you get your result 01/07/2015. So SimpleDateFormat works correct.
One solution for you is to change your date pattern to M/d/yyyy h:m:s a or your input data.
To avoid these tries you have to switch off SimpleDateFormat lenient mode. Then you will get an exception if the format does not fit.
It looks like your input format is actually months first, then days. So should be "MM/dd/yyyy".
So:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/M/yyyy");
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDocumentDate = dateFormatter.format(dateParser.parse(sysObj.getString("document_date")));
Another example like this
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMdd");
System.out.println("date is:"+new java.sql.Date( sdfInput.parse("20164129").getTime() ));
Output is: 2019-05-29
I expect to throw parse exception but not (41)is not a valid month value.
on the other hand if I gave 20170229, system can recognize the February of 2017 doesn't have a lap year and return 2017-03-01 interesting.
Related
I'm using a combination of DateFormat and SimpleDateFormat to achive following string from a Date object:
fre 20 22:48
This snippet produces the outcome above:
DateFormat localizedTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
String str = new SimpleDateFormat("EE d", Locale.getDefault()).format(date) + " " + localizedTimeFormatter.format(date);
The outcome is exactly as I want, but I think there are improvements that could be done. I would like to use only the SimpleDateFormat to achieve this and getting shortname for day in week and day of month are not the problem. The problem is getting time in only hours and minutes according to locale, or more correctly, 12 or 24 hour format. I've checked out the documentation on the SimpleDateFormat Javadoc SimpleDateFormat
but as I can see there are no way of getting a time(hhmm) in 12 or 24hour format just by setting a format string to the SimpleDateFormat? Are there any way to achieve this by only using SimpleDateFormat as a oneliner, or do I have to do it the way I have done already, or are there a completely different way of solving this little issue?
How about
SimpleDateFormat sdf = new SimpleDateFormat("EE d HH:mm", Locale.getDefault());
or
SimpleDateFormat sdf = new SimpleDateFormat("EE d hh:mm", Locale.getDefault());
Your method is on the right track.
However, I didn't need to use the DateFormatter.
I got your expected result in this line itself.
String str = new SimpleDateFormat("EEE, d, k:m", Locale.getDefault()).format(Calendar.getInstance().getTime());
Output is:
Mon, 30, 19:5
The pattern you use in the first argument to SimpleDateFormat is the key here I think.
The meaning of the individual alphabets and their combination is already mentioned in the link you gave.
Hope this helps.
I have used the Calendar class to get the current Date. Now I want to convert that date to Date format so that it can be stored in database with format "yyyy.mm.dd". I tried to convert this using SimpleDateFormat class
String dateString = dateText.getText();
SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy.mm.dd ");
Date convertedDate = dateFormat.parse(dateString);
but I couldn't convert into Date type.
Try to remove spaces from the format string
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.mm.dd");
Also if your input date has invalid format you might get a parse exception. Better if you put it into try/catch block.
Notice, that m stands for minute in hour but M for month of year. Make sure you put a valid format pattern.
You havent stated what the error is but its unlikely that you want to use a minute field to parse the month. Use uppercase M:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
get rid of the whitespaces in your pattern
In my HTML part I have text box where the date is entered as: 10.09.2013 (dd.mm.yyyy)
What I'm doing in my servlet is:
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
String dateInString = invoiceData.getString("date");
Date date = formatter.parse(dateInString);
System.out.println(date);
The system prints: Thu Jan 10 00:09:00 EET 2013 and inside of the oracle database it's inserted as 10-SEP-2013
Please help me! I'm dealing with it for about 2 hours and I really can not find a way to convert it and insert it correctly in my database. I know that I'm missing something super small, but I'm unable to spot it.
I'm using Jboss 7.1 server.
new SimpleDateFormat("dd.MM.yyyy");
instead of
new SimpleDateFormat("dd.mm.yyyy");
mm stands for Minutes. MM stands for Month
Take a look date format
mm is used for minutes and MM is used for Month
SO you need to change:
new SimpleDateFormat("dd.MM.yyyy");
mm stands for minutes.
You need to use MM in your pattern to get the month.
mm for Minutes and MM for Month. Change the format to dd.MM.yyyy get the correct date.
Change your code like below:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
String dateInString = invoiceData.getString("date");
Date date = formatter.parse(dateInString);
System.out.println(date);
use following format
new SimpleDateFormat("dd.MM.yyyy");
This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have date like Tue Mar 19 00:41:00 GMT 2013, how to convert it to 2013-03-19 06:13:00?
final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = bdate;
Date ndate = formatter.parse(formatter.format(date));
System.out.println(ndate);
gives the same date.
Use two SimpleDateFormat objects with appropriate formats and use the first to parse the string into a date and the second to format the date into a string again.
As the first answer says. First parse your date with SimpleDateFormat like this:
Date from = new SimpleDateFormat("E M d hh:mm:ss z yyyy").parse("Tue Mar 19 00:41:00 GMT 2013");
Then use that to format the resulting date object with another instance of SimpleDateFormat like this:
String to = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(from);
See the javadoc of SimpleDateFormat here. Hope that helps.
One major thing that the others have left out is dealing with the timezone (TZ). Anytime you use a SimpleDateFormat to go to/from a string representation of the date, you really need to be aware of what TZ you're dealing with. Unless you explicitly set the TZ on the SimpleDateFormat, it will use the default TZ when formatting/parsing. Unless you only deal with date strings in the default timezone, you'll run into problems.
Your input date is representing a date in GMT. Assuming that you also want the output to be formatted as GMT, you need to make sure to set the TZ on the SimpleDateFormat:
public static void main(String[] args) throws Exception
{
String inputDate = "Tue Mar 19 00:41:00 GMT 2013";
// Initialize with format of input
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
// Configure the TZ on the date formatter. Not sure why it doesn't get set
// automatically when parsing the date since the input includes the TZ name,
// but it doesn't. One of many reasons to use Joda instead
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse(inputDate);
// re-initialize the pattern with format of desired output. Alternatively,
// you could use a new SimpleDateFormat instance as long as you set the TZ
// correctly
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
}
Use SimpleDateFormat in this way:
final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = new Date();
System.out.println(formatter.format(date));
If you do any calculations or parsing with dates please use JodaTime because the standard JAVA date support is really buggy
how to format "2011-10-25T13:00:00Z" string into date and time
i used simple date format class
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
but it only giving the date value. not time values
please help me to solve this problem
Use the format "yyyy-MM-dd'T'HH:mm:ss'Z'" for parsing this date format. See the documentation of SimpleDateFormat for more info. Code will look like this
String dateStr = "2011-09-19T15:57:11Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Date date = new SimpleDateFormat(pattern).parse(dateStr);
This is because "yyyy-MM-dd" only mentions year (yyyy), month (MM) and date (dd). Try adding hh:mm if you want hours and minutes.
Example:
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm");
System.out.println(sim.format(new Date())); // prints "2011-10-27 01:56"
The full documentation of the format-string and its parts is found here. The documentation includes this example:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" - 2001-07-04T12:08:56.235-0700
Perhaps it's something like that you're looking for.