I am trying to parse a date in Korean Date format using SimpleDateFormat which works. However i would like to remove any dependency on adding up Korean Characters in Pattern for Year(년), Month(월) and Day(일) and so on.
String dateinKorean = "2013년 9월 26일 (목)";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy'년' M'월' d'일' '('EE')'", Locale.KOREA);
try {
Date dt = sdf.parse(dateinKorean);
System.out.println(dt.toGMTString());
} catch (ParseException e) {
e.printStackTrace();
}
I am trying to use DateFormatSymbols Class to Parse the date using the Locale, however the problem is I am not able to parse the complete date, I can parse an individual Month(MM), Year(yyyy) or Day(dd) without any issues.
DateFormatSymbols df = DateFormatSymbols.getInstance(Locale.KOREAN);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd", df);
try {
// 2013년 9월 26일
Date dt = sdf.parse("2013년 9월 26일");
} catch (ParseException e) {
}
Can anyone please help me identify if there is any other way to parse the dates other than shown above ?
If the date format includes "fixed" characters they are used as-is, so if you include words like "year" or "month" in the format they cannot be ignored.
You can hack your way around this by removing all non-digits from the string before parsing, for example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", df);
Date dt = sdf.parse("2013년 9월 26일".replaceAll("\\D+", "-"));
A better solution could be internationalizing the date format so that each language you support could have a format of its own.
Related
I'm trying to parse a String into Data, I create the DataParser, in according to date format, the code I wrote is this:
String date_s = "04-May-2017 17:28:27";
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date;
try {
date = formatter.parse(date_s);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
When I execute this, I got always an exception
java.text.ParseException: Unparseable date: "04-May-2017 17:28:27"
I don't understand why the data is not parsed, someone can help me?
This thread of answers would not be complete without the modern solution. These days you should no longer use Date and SimpleDateFormat, but switch over to the newer date and time classes:
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime dateTime;
try {
dateTime = LocalDateTime.parse(date_s, formatter);
System.out.println(dateTime);
} catch (DateTimeParseException e) {
System.err.println(e);
}
This prints
2017-05-04T17:28:27
(LocalDateTime.toString() returns ISO 8601 format) If leaving out Locale.ENGLISH, on my computer I get
Text '04-May-2017 17:28:27' could not be parsed at index 3
Index 3 is where it say May, so the message is somewhat helpful.
LocalDateTime and DateTimeFormatter were introduced in Java 8, but have also been backported to Java 6 and 7.
the string you want to parse is local dependent (the word May is English), so the jvm is not able to infer that may is the month of may in English
define the formatter using the constructor qith the locale.
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss",Locale.ENGLISH);
You need another constructor with a Locale that supports MMM (May)
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss",Locale.US)
or using standard format dd-MM-yyyy with month digits.
(Sorry, in the meantime the answer was already posted)
I am using jQuery Datepicker that is giving the date like 07/05/2015 this format.I am using simpledateformat to format this date.But always the SDF is converting it to the todays date.How to solve this ??
System.out.println("Activity IS : IS With Date");
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd ");
Date startDate = ParamUtil.getDate(resourceRequest, "startDate", sdfOverTimeWithDate);
Date endDate = ParamUtil.getDate(resourceRequest, "endDate", sdfOverTimeWithDate);
int jobId= ParamUtil.getInteger(resourceRequest, "jobId");
System.out.println("jobId :"+jobId);
System.out.println("startDate :"+startDate);
System.out.println("endDate :"+endDate);
This statDate and endDate is giving todays's date only ,while the date i am pssing is in the format 07/05/2015.How to solve this ??somebody plaese help
You are passing wrong date format to SimpleDateFormat constructor. Try "dd/MM/yyyy" instead of "yyyy-MM-dd "
Is "07/05/2015" in July (US-Format) or May? Please clarify. If it is US-format (date expression starting with month number) then your solution is to use the pattern "MM/dd/yyyy" otherwise you should use the pattern "dd/MM/yyyy".
The pattern "yyyy-MM-dd " cannot be right due to two reasons:
a) It starts with a four-digit-year but your input begins with a two-digit-number.
b) It contains a trailing space.
Try to do it.
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdfOverTimeWithDate.parse("07/05/2015");
} catch (ParseException e) {
e.printStackTrace();
}
I am trying to format dates entered by my application user using SimpleDateFormat but I always get an error:
01/28/2014java.text.ParseException: Unparseable date: "01/28/2014"
The code I am using to format the date is as follows:
Date rDate, dDate;
String Date1 = request.getParameter("Date1");
String Date2 = request.getParameter("Date2");
//Here the date get display for example as 01/29/2014 (i.e. MM/DD/YYYY)
System.out.println("Date1:: "+ Date1);
System.out.println("Date2:: "+ Date2);
SimpleDateFormat parseRDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat parseDDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
//#########Crashes in the next two lines#########...
rDate = (Date)parseRDate.parse(Date1);
dDate = (Date)parseDDate.parse(Date2);
} catch (ParseException e) {
e.printStackTrace();
}
Can someone please help me by telling me what I am doing wrong here?
Thanks for your time
You need to match the DateFormat pattern to your input String
SimpleDateFormat parseRDate = new SimpleDateFormat("MM/dd/yyyy");
Any idea how I can convert the format from MM/dd/YYYY to yyyy-MM-dd HH:mm:ss?
All you need to do is use a separate SimpleDateFormat instance for formatting
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(output.format(date1));
As you say, Date1 is of the form MM/dd/yyyy... but you're trying to parse it with a format of yyyy-MM-dd HH:mm:ss.
The pattern you parse to the SimpleDateFormat constructor has to match the format of the data itself. (What did you think you were specifying in the constructor?)
Note that the code you've provided isn't doing and formatting at all - just parsing.
You should also work out which time zone you're interested in, and which Locale. Personally I think it's clearer to specify both of those explicitly - even if you want the system default ones.
(If you're doing any significant amount of date/time work, you should also consider using Joda Time, which is a much more pleasant date/time API. I'd also consider more useful exception handling, and following Java naming conventions...)
You specify the format yyyy-MM-dd HH:mm:ss in the constructor and then accept a completely different format (MM/dd/yyyy) as input. You need to make the actual format match the expected format.
Examine the following (for comparison):
Date rDate, dDate;
SimpleDateFormat parseRDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat parseDDate = new SimpleDateFormat("MM/dd/yyyy");
try {
rDate = (Date)parseRDate.parse("2014-01-28 12:22:22");
dDate = (Date)parseDDate.parse("01/28/2014");
} catch (ParseException e) {
e.printStackTrace();
}
The string passed to the constructor is what tells SimpleDateFormat how to read the input you give it later.
I am trying to develop an android application, regarding with that I have got some data from server.
Here I need to convert a date which comes in the given format "2013-12-31T15:07:38.6875000-05:00"
I need to convert this in to a date object or a calendar object how can we do this?
I have tried with code given below. But it doesn't reach my expectation
String dateString="2013-12-31T15:07:38.6875000-05:00";
Date date = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
try {
date = df.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
Your format string doesn't match the date format you are getting. You should use the following format string:
"yyyy-MM-dd'T'HH:mm:ss.SSSX"
SSS is for milliseconds.
X is for ISO 8601 timezone
Step 1:
As you are getting 2013-12-31T15:07:38.6875000-05:00 as a date value, you need to define a format to parse the date.
String myDateString= "2013-12-31T15:07:38.6875000-05:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
date = df.parse(myDateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Step 2:
Once you parse the date, you can format it into desired format.
For example: Let's convert the parsed Date object into the yyyy-MM-dd value.
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String strNewDate = newFormat.format(date);
You can use the following format for your string formatter
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Source : SimpleDateFormat ignoring month when parsing
For reading : http://developer.android.com/reference/java/text/SimpleDateFormat.html
use this code
String string = "2013-12-31T15:07:38.6875000-05:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Simple date formats
If you were using the Joda-Time library, you could skip the formatter and feed the ISO 8601 string directly to the constructor of a DateTime object.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
DateTime dateTime_DefaultTimeZone = new DateTime( "2013-12-31T15:07:38.6875000-05:00" );
Output to localized time zone and language…
String localizedOutput = DateTimeFormat.forStyle("LS").withLocale(Locale.CANADA_FRENCH).withZone( DateTimeZone.forID( "America/Montreal" ) ).print( dateTime_DefaultTimeZone );
Dump to console…
System.out.println( "dateTime_DefaultTimeZone: " + dateTime_DefaultTimeZone );
System.out.println( "localizedOutput: " + localizedOutput );
When run…
dateTime_DefaultTimeZone: 2013-12-31T12:07:38.687-08:00
localizedOutput: 31 décembre 2013 15:07
How can I parse the following date in Java from its string representation to a java.util.Date?
2011-07-12T16:45:56
I tried the following:
private Date getDateTime(String aDateString) {
Date result = new java.util.Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
try
{
result = sf.parse(aDateString);
}
catch (Exception ex)
{
Log.e(this.getClass().getName(), "Unable to parse date: " + aDateString);
}
return result;
}
You are not using the right date format pattern. The year/month/day separators are clearly wrong, and you need a literal 'T'. Try this:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
For the record, this is an ISO 8601 combined date and time format.
Your date format pattern is incorrect. It should be yyyy-MM-dd'T'HH:mm:ss .
To parse a date like "2011-07-12T16:45:56" you need to use:-
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Check the pattern you are feeding your SimpleDateFormat against the string you are feeding in.
See any potential discrepancies? I see 3.