Can SimpleDateFormat format the date based on a string? - android

I know how SimpleDateFormat works, by grabbing sdfDateTime.format(new Date(System.currentTimeMillis())); which is today's date from the System. Can SimpleDateFormat format a date by grabbing a string? Let's say you had a string: String dateStr = "04/05/2012"; how would you format that into: "April 5, 2012"?

DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
outputFormat.setLenient(false);
String inputDateAsString = "04/05/2012";
Date inputDate = inputFormat.parse(inputDateAsString);
System.out.println(outputFormat.format(inputDate));
You can't just grab an arbitrary string and figure out what its format is.

check this one, it is very helpful library, easy to use & extend for such needs
https://bitbucket.org/dfa/strtotime/wiki/Home

Related

Simpledateformat is not giving proper date

I want today's date in my application to store it in a database as a string but when i used simpledatefromat and calender.getInstance(), it's not giving me proper date. The code is as below.
I tried simpledateformat's another constructor which takes locale as parameter but still not showing proper date
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
String dateF = dateFormat.format(date);
expected date should be 21-04-2019
actual result 21-26-2019
To get the month like the way that you want, you need to change "dd-mm-yyyy" to "dd-MM-yyyy"
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String dateF = dateFormat.format(date);

Android how to convert string into datetime format?

I have in String variable this ... "2015-01-12 19:00:00" So I want to convert to DateTime value , I have tried with this....
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss");
Date d = dateFormat.parse(fecha);
But I can't compile, I have gotten this message ... cannot convert from java.util.Date to java.sql.Date
How fix it?
The pattern is actually wrong, try this
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = dateFormat.parse(fecha);
Check if you have imported java.sql.Date instead of java.util.Date.

How can I convert String Date Format in Android?

I have a string that contains a date like so:
String startTime = "2014-10-11T17:00:41+0000"
I am trying to reformat that string so that it reads like so instead:
Oct 11, 2014 5:00 PM
Since Date objects do not keep time zone information, you need to specifically set the time zone offset of original date to the target formatter. Here is the complete code for transforming from one format to another while maintaining the time zone offset (+0000 in your case). More information on TimeZone class here and on how to build a proper date and time pattern string for your requirement here.
try {
DateFormat originalFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat(
"MMM dd, yyyy K:mm a", Locale.ENGLISH);
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Date date = originalFormat.parse("2014-10-11T17:00:41+0000");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: Oct 11, 2014 5:00 PM
Use SimpleDateFormat for parse input string and represent in new format:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ex.:
SimpleDateFormat sdfmtIn = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat sdfmtOut= new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date date = sdfmtIn.parse(strInput);
String strOutput = sdfmtOut.format(date);

how to format a date?

Hello I have been trying to format this date but it keeps giving me in unparsable date error? I am trying to get a time stamp like 2011-06-24T19:55:37Z to be June 24, 2011. here is the code I am using. Also on a side note is contraction (like the 1st, 2nd, 3rd) possible?
SimpleDateFormat sdf = new SimpleDateFormat("MM d, yyyy", Locale.US);
Date dt = sdf.parse("2011-03-01T17:55:15Z");
time.setText("Time: " + dt.toString());
The problem is that the format provided to SimpleDateFormat's constructor doesn't match the format of your date.
The string MM d, yyyy tells SimpleDateFormat how to interpret 2011-03-01T17:55:15Z.
Building a format string is described in the docs.
This comes from another question within stackoverflow
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS -05:00");
Date date = (Date)formatter.parse("2011-06-24T19:55:37Z");
Make sure the SimpleDateFormat matches your string

how do i change the format of a string representing time in android?

I have a string which i retrieve from the database which is of the following format
30-08-2010 12:30:00
i need to convert that string into the following format
Aug 30 2010 12:30 PM
How do i do this?
Also tell me how to convert the string into a date object.
thank you in advance.
look around java.util.* for more info. It has nothing to do with Android.
DateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = (Date)formatter1.parse("30-08-2010 12:30:00");
DateFormat formatter2 = new SimpleDateFormat("MMM dd yyyy hh:mm a");
String newFormattedString = formatter2.format(date);

Categories

Resources