Android date format using SimpleDate - android

I am working on an XML Parser for Android with twelve different items and I need help creating a date for each item. This is what I have so far:
TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate);
I am looking to make the date look like this: Saturday, September 3. Thank you for your help!

If you are looking for todays date, you can do this by using the Date object.
Date today = new Date();//this creates a date representing this instance in time.
Then pass the date to the SimpleDateFormat.format(Date) method.
// "EEEE, MMMM, d" is the pattern to use to get your desired formatted date.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM, d");
String formattedDate = sdf.format(today);
Finally, you can set this String into your TextView
detailsPubdate.setText(formattedDate);
Here is the documentation for SimpleDateFormat. The documentation shows the various patterns available to format Date objects.

use the DateFormat class to format your dates.
Example:
DateFormat formatter = new SimpleDateFormat("EEE, MMM d");
Date date = (Date) formatter.parse("02.47.44 PM");
detailsPubdate.setText(date.toString());
This is the java documentation for SimpleDateFormatter if you want to change your pattern.

It is not very clear what you are trying to do, but you could use the following to format date:
SimpleDateFormat sdf=new SimpleDateFormat("EEEE, MMMM, d");
//get current date
Date date=new Date();
String dateString=sdf.format(date);
You should have a look at the SimpleDateFormat class.

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);

how to convert yyyy-mm-ddThh:mm:ss+05:30 to hh:mm:ss AM/PM in android

I am receiving date and time format as mentioned below
2016-04-13T00:12:33+05:30
i need to convert above format to HH:MM:SS AM/PM
can any one guide me,your response will be appreciated......
Android has the DateFormat class for parsing and reformatting dates as Strings, but JodaTime is much better. JodaTime is small and can handle what you're looking for in just a couple lines of code.
You need first String to Date using Simple forma tor
Then date need to change to formatResult fro expected result
SimpleDateFormat format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss", Locale.ENGLISH);
SimpleDateFormat formatResult = new SimpleDateFormat("hh:mm aa");
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Log.i("date ====== " + formatter.format("2016-04-13T00:12:33+05:30"));

Parse exception in Android

I am using SimpleDateFormat class to parse date as follows :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
But, getting the Parse exception error as follows :
java.text.ParseException: Unparseable date: "2015-09-16 10:21 AM" (at offset 16)
I have checked another format as a argument in SimpleDateFormat class is :
"yyyy-MM-dd HH:mm:ss a" but, still same error is coming.
Is there any other datetime format I have to pass as a argument of SimpleDateFormat class ?
As you have said in the above comments, you use a TimePicker for choosing a date time and parse it. And you said that you also want the second. But apparently, TimePicker doesn't include the second. So how are you going to know what second the user wants? I guess 0. You can insert a string into the correct position to make it work. In you case you should insert ":00" in index 16. Just use a StringBuilder!
StringBuilder builder = new StringBuilder("2015-09-16 10:21 AM");
builder.insert (16, ":00");
Try this if your input has not second value ..
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
And if it has second value also then use this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
otherwise above one
For More detail....
TimePicker widget doesn't provide year-month-day , it only provide hour and minutes
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
To get year-month-day you have to use DatePicker widget

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

Get user friendly date android

Hi i want to Get System Date In User Friendly Manner Like February 14, 2011 12:00 AM. How can I do this in android?
Here you can use SimpleDateFormat class in which you can get Date and time as you want. You can write something like this
Configuration mConfiguration = new Configuration();
Settings.System.getConfiguration(getContentResolver(), mConfiguration);
final TimeZone mTimeZone = Calendar.getInstance(mConfiguration.locale).getTimeZone();
SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("MM.dd HH:mm");
mSimpleDateFormat.setTimeZone(mTimeZone);
mRefreshDateTime = mSimpleDateFormat.format(new Date());
Where in SimpleDateFormat you can pass as you like. You can find the class parameters from here: SimpleDateFormat
I hope it would help you.
Calendar mReminderCal = Calendar.getInstance();
mReminderCal.setTimeInMillis(System.currentTimeMillis());
Date dateText = new Date(mReminderCal.get(Calendar.YEAR)-1900,
mReminderCal.get(Calendar.MONTH),
mReminderCal.get(Calendar.DAY_OF_MONTH),
mReminderCal.get(Calendar.HOUR_OF_DAY),
mReminderCal.get(Calendar.MINUTE));
String dateString = android.text.format.DateFormat.format("MM/dd/yyyy hh:mm", dateText));
Use SimpleDateFormat.
SimpleDateFormat like in Java SE

Categories

Resources