How to parse 'dd MMM yyyy' - java

How to convert date fromat from "dd MMM yyyy" to "yyyy-MM-dd"?
I know I have to use SimpleDatFormat but it doesn't work, neither does any solution from similar questions.
I have a date "18 Dec 2015" that I am trying to format but I get this
java.text.ParseException: Unparseable date: "18 Dec 2015"
Here's my code:
public String parseDate(String d) {
String result = null;
Date dateObject = null;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
try {
dateObject = dateFormatter.parse(d);
dateFormatter.applyPattern("yyyy-MM-dd");
result = dateFormatter.format(dateObject);
} catch (ParseException e) {
System.out.println(e);
}
return result;
}

Did you try
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
instead of
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
(note the hyphens, since your pattern doesn't match your input)
Also helpful: using Locale.US as recommended by #ZouZou

You are passing input as "18-Dec-2015" instead of the form "dd MMM yyyy". Try and pass input like 18 Dec 2015 and it should work.

Related

How to convert Date to a particular format in android?

"Mar 10, 2016 6:30:00 PM" This is my date and I want to convert this into "10 Mar 2016". Can I use SimpleDateFormat in android. I am not getting the exact pattern to convert it. Please help and thanks in advance
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("Some Pattern for above date");
Date newDate=spf.format(date);
spf= new SimpleDateFormat("dd MMM yyyy");
String date = spf.format(newDate);
Will this steps work? If yes, can someone please give me a pattern of that format? Thanks in advance.
This is modified code that you should use:
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("dd MMM yyyy");
date = spf.format(newDate);
System.out.println(date);
Use hh for hours in order to get correct time.
Java 8 and later
Java 8 introduced new classes for time manipulation, so use following code in such cases:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a");
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println(dateTime.format(formatter2));
Use h for hour format, since in this case hour has only one digit.
conversion from string to date and date to string
String deliveryDate="2018-09-04";
SimpleDateFormat dateFormatprev = new SimpleDateFormat("yyyy-MM-dd");
Date d = dateFormatprev.parse(deliveryDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE dd MMM yyyy");
String changedDate = dateFormat.format(d);
You can use following method for this problem. We simply need to pass Current date format, required date format and Date String.
private String changeDateFormat(String currentFormat,String requiredFormat,String dateString){
String result="";
if (Strings.isNullOrEmpty(dateString)){
return result;
}
SimpleDateFormat formatterOld = new SimpleDateFormat(currentFormat, Locale.getDefault());
SimpleDateFormat formatterNew = new SimpleDateFormat(requiredFormat, Locale.getDefault());
Date date=null;
try {
date = formatterOld.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
result = formatterNew.format(date);
}
return result;
}
This method will return Date String in format you require.
In your case method call will be:
String date = changeDateFormat("MMM dd, yyyy hh:mm:ss a","dd MMM yyyy","Mar 10, 2016 6:30:00 PM");
You should parse() the String into Date and then format it into the desired format. You can use MMM dd, yyyy HH:mm:ss a format to parse the given String.
Here is the code snippet:
public static void main (String[] args) throws Exception
{
String date = "Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
Date newDate = spf.parse(date);
spf = new SimpleDateFormat("dd MMM yyyy");
String newDateString = spf.format(newDate);
System.out.println(newDateString);
}
Output:
10 Mar 2016
For the sake of completeness, here is the modern version. This is for anyone reading this who either uses Java 8 or later or is happy with a (good and futureproof) external library.
String date = "Mar 10, 2016 6:30:00 PM";
DateTimeFormatter parseFormatter
= DateTimeFormatter.ofPattern("MMM d, uuuu h:mm:ss a", Locale.ENGLISH);
DateTimeFormatter newFormatter
= DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH);
date = LocalDateTime.parse(date, parseFormatter).format(newFormatter);
System.out.println(date);
This prints the desired
10 Mar 2016
Please note the use of explicit locale for both DateTimeFormatter objects. “Mar” and “PM” both are in English, so neither the parsing nor the formatting will work unless some English-speaking locale is used. By giving it explicitly we are making the code robust enough to behave as expected also on computers and JVMs with other default locales.
To use the above on Android, use ThreeTenABP, please see How to use ThreeTenABP in Android Project. On other Java 6 and 7 use ThreeTen Backport.
You need to use SimpleDateFormat class to do the needful for you
String date = "Your input date"
DateFormat originalFormat = new SimpleDateFormat("<Your Input format here>", Locale.US)
DateFormat targetFormat = new SimpleDateFormat("<Your desired format here>", Locale.US)
Date Fdate = originalFormat.parse(date)
formattedDate = targetFormat.format(Fdate)
public static String formatDate(String fromFormat, String toFormat, String dateToFormat) {
SimpleDateFormat inFormat = new SimpleDateFormat(fromFormat);
Date date = null;
try {
date = inFormat.parse(dateToFormat);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat(toFormat);
return outFormat.format(date);
}
Use:
formatDate("dd-MM-yyyy", "EEEE, dd MMMM yyyy","26-07-2019");
Result:
Friday, 26 July 2019

Convert a date from 14 to 2014

How can I format a :
Tue May 21 00:00:00:00 GMT +200 14 <--- Tue May 21 00:00:00:00 GMT
+200 2014
i tried :
StringBuilder myName = new StringBuilder(datum);
myName.setCharAt(datum.length()-4, '2');
myName.setCharAt(datum.length()-3, '0');
Date date= null; DateTimeFormat.getFormat("-- idk ----").parse(myName.toString()); Window.alert(myName.toString());
but i dont know how to define the same date format as the Date class
i think this isnt a good solution is there a better?
I suppose that datum is a Date object.
So, just do:
DateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss:SS z yyyy");
System.out.println(df.format(datum));
More info: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
// getFormatedDate("in which pattern you are sending date", "how we want", date in string form);
example : formatedDate = getFormatedDate("yyyy-MM-dd", "ddMMyyyy", "2014-05-21");
private String getFormatedDate(String baseFormat, String reqFormat, String dateStr) {
String formatedDate = null;
try {
DateFormat fromFormat = new SimpleDateFormat(baseFormat);
fromFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat(reqFormat);
toFormat.setLenient(false);
java.util.Date date = fromFormat.parse(dateStr);
formatedDate = toFormat.format(date);
} catch (ParseException ex) {
}
return formatedDate;
}
You have to use date formatter.
Your date formatter should be
DateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss:SS z yyyy");
Then use this formatter to get your desired date.
Just do:
newDate = dateFormat.format(yourOldDate);
Hope this will works.
Get more from:
http://www.mkyong.com/java/java-date-and-calendar-examples/
and http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Thanks.

Date format conversion error [duplicate]

This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 9 years ago.
I am facing the problem while converting the date:
Current format is:Thu Sep 05 12:07:46 IST 2013(dow mon dd hh:mm:ss zzz yyyy)
I need to convert in to:09/04/2013 11:38 PM PDT(mm/dd/yyyy hh:mm a zzz)
But i am not able to convert.
Try using SimpleDateFormatter. You have to tell it the input/output format, you can do that based on this description (you can also find a few common examples there).
The code will be something like this:
try {
String input = "Thu Sep 05 12:07:46 IST 2013";
DateFormat formatter = new SimpleDateFormat("I leave this to you :-)))");
System.out.println(formatter.parse(input));
} catch (ParseException e) {
e.printStackTrace();
}
Hope that helps.
You can do this
TimeZone tz = TimeZone.getTimeZone("PST8PDT"); // example
// required format. Remember M is for month, m for miniute
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a zzz");
df.setTimeZone(tz);
String text = df.format(new Date());// current time
System.out.println(text);
Also please check this TimeZones in Java
You try to convert dateformat and timeZone as well, so you need to convert the timezone in your code.
SimpleDateFormat sf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
isoFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = isoFormat.parse("mm/dd/yyyy hh:mm a zzz");
this may help you.
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
OutPut
09/05/2013 00:07:46 AM IST
update
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
dfto.setTimeZone(zone);
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
output
09/04/2013 11:37:46 AM PDT

Date format issue in android

In android
am getting date in
(date = "04-01-2013") this format
but i want to show same date in
en.US format like (date="Friday,January 04,2013")
use SimpleDateFormat.
set input pattern matching input date string: "04-01-2013" -> "dd-MM-yyyy".
And output pattern like output: "Friday, January 04, 2013" -> "EEEE, MMMM dd, yyyy"
public String formatDate(String input){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date d = sdf.parse(input);
sdf.applyPattern("EEEE, MMMM dd, yyyy");
return sdf.format(d,new StringBuffer(),0).toString();
}
try {
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy");
return df2.format(df1.parse(input));
}
catch (ParseException e) {
return null;
}
You can use something like this
android.text.format.DateFormat.format("EEEE, MMMM dd, yyyy", new java.util.Date());
Take a look at DateFormat.
This is how you do it in java
SimpleDateFormat df=new SimpleDateFormat("EEEE,MMMM dd,yyyy");
java.util.Date date=df.parse("04-01-2013");
refer this

Twitter date unparseable?

I want to convert the date string in a Twitter response to a Date object, but I always get a ParseException and I cannot see the error!?!
Input string: Thu Dec 23 18:26:07 +0000 2010
SimpleDateFormat Pattern:
EEE MMM dd HH:mm:ss ZZZZZ yyyy
Method:
public static Date getTwitterDate(String date) {
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
Date twitterDate = null;
try {
twitterDate = sf.parse(date);
} catch (Exception e) {}
return twitterDate;
}
I also tried this: http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ but that gives the same result.
I use Java 1.6 on Mac OS X.
Cheers,
Andi
Your format string works for me, see:
public static Date getTwitterDate(String date) throws ParseException {
final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
return sf.parse(date);
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));
}
Output:
Fri Dec 03 18:26:07 GMT 2010
UPDATE
Roland Illig is right: SimpleDateFormat is Locale dependent, so
just use an explicit english Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
This works for me ;)
public static Date getTwitterDate(String date) throws ParseException
{
final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
sf.setLenient(true);
return sf.parse(date);
}
Maybe you are in a locale where ‘Tue‘ is not a recognized day of week, for example German. Try to use the ‘SimpleDateFormat‘ constructor that accepts a ‘Locale‘ as a parameter, and pass it ‘Locale.ROOT‘.
You should not have ZZZZZ but only Z for the timezone.
See samples in http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for more information.
EEE, d MMM yyyy HH:mm:ss Z > Wed, 4 Jul 2001 12:08:56 -0700
SimpleDateFormat is not thread safe. "EEE MMM dd HH:mm:ss ZZZZZ yyyy" was working in our application, but failing in a small percentage of cases. We finally realized that the issue was coming from multiple threads using the same instance of SimpleDateFormat.
Here is one workaround: http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html
Function for convert Twitter Date :
String old_date="Thu Jul 05 22:15:04 GMT+05:30 2012";
private String Convert_Twitter_Date(String old_date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
SimpleDateFormat old = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",Locale.ENGLISH);
old.setLenient(true);
Date date = null;
try {
date = old.parse(old_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sdf.format(date);
}
The output format like : 05-Jul-2012 11:54:30

Categories

Resources