Display different Date Format - android

my date format is display like this :
Date : 19/01/2015(January)
Here is the code :
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
String day = cal.getDisplayName(dayOfWeek, Calendar.LONG, Locale.US);
But I want to display in like this :
Date : 19/01/2015(Mon)
How can i modify the codes? Please help me take a look.Thanks!

You can do it standard Java way
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy(EEE)")
dateFormat.format(new Date())

Please use below three lines:
String format = "dd-MM-yyyy(EEE)";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
System.out.format("%30s %s\n", format, sdf.format(new Date(0)));
Logs - 01-21 16:39:34.125: I/System.out(11052): dd-MM-yyyy(EEE) 01-01-1970(Thu)

Related

Formatting date from a DatePicker

I know I can get the date from a datepicker using the following methods:
datepicker1.getDayOfMonth()
datepicker2.getMonth()
datepicker3.getYear()
I am also getting the time from a timepicker using the following methods:
timepicker1.getCurrentHour()
timepicker2.getCurrentMinute()
I need to format this data into "yyyy-MM-dd HH:mm:ss" format to place it into an sqlite database as a DATETIME variable. Is there a method which can handle this?
I think I will have to build it manually like so:
if(month < 10){
month = "0" + month;
}
if(day < 10){
day = "0" + day ;
}
The problem was that the months and days were single digit.
SimpleDateFormat outputFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date ModifiedDate = outputFormat.parse(strdate);
Simple as below:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.parse(yourDateHere);
You can get Current Date by this following and save it to the string
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
now pass this string to the Simpledateformat class and define your desired Format like following
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(mydate);

android SimpleDateFormat("dd/mm/yyyy") not working well

I have this function
private void setDateInTopBar() {
Date bodyPartDate = DataManager.instance().getSelectedBodyPart().getPublicationDate();
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String formattedDate = format.format(bodyPartDate);
TextView dateTxt = (TextView) findViewById(R.id.txtImageDate);
dateTxt.setText(formattedDate);
}
but my month is 0, when day and year is working well, what's the problem?
Replace:
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
With:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
as mm referes to minutes.. check http://developer.android.com/reference/java/text/SimpleDateFormat.html
See http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
lower-case m is for minutes.
Try: "dd/MM/yyyy"
I should set MM not mm here
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
mm means Minutes, So you shouldn't use it for Month, change mm to MM.
Your code must be like this:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Instead of this:
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
You can read all about these letters here

Can SimpleDateFormat format the date based on a string?

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

Date comparison in Android application

I have 2 dates in string format like DD/MM/YYYY. Now I have to write the condition if date1 is before date2. How can I do this? Please help me.
Try this:
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = curFormater.parse(date1Str);
Date date2 = curFormater.parse(date2Str);
if (date1.before(date2))
{
}

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...

Categories

Resources