get date format from given date in android - android

Hello i am new in android. Can anybody help me to find how i get date format Sun 15th July from 2012-07-15? I found examples but each of it is for various different date-formats. Please help me to find this.

try to get idea from my another answer in [Convert Date time in “April 6th, 2012 ” format][1]
[1]: Convert Date time in "April 6th, 2012 " format here you can only change the April to Sun and 2012 to month.
Like:-
DateFormatSymbols symbols = new DateFormatSymbols();
symbols.setShortWeekdays(new String[]{"Sun","Mon"....});
SimpleDateFormat format = new SimpleDateFormat("EE", symbols);
from this code you will get Days in "Sun,Mon,Tue..." Short Format
So pass this symbols in your SimpleDateFormat class.

Related

How To See If One Date Is Newer Than Another

Hello I think I have a very simple question but i'm having trouble figuring it out.
I Got the Date and Time In Android using this code
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
It gave me back this value
Apr 21, 2016 9:30:16 PM
how do I compare using to dates with that value so if I want to see if
Apr 21, 2016 9:30:16 PM
is newer or older than
Apr 21, 2016 9:35:16 PM
How would I check that Thanks
Attempt One
I Tried This
DateFormat format = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Date fileDate = format.parse(date1);
DateFormat format2 = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Date metaDate = format.parse(date2);
this the value for date 1 and 2 being
Apr 21, 2016 9:35:16 PM
But it threw a parse exception. I must use that value above so What do I Need to do so it doesn't break the code when it tries to parse the date
The easiest way is to use Date.before(), rather than comparing the strings. In fact its easier to convert the string back into a date than use the strings.

joda-time can't read Parse dateTime string

I'm having an issue with joda-time formatting a date time string that Parse (parse.com) has stored in an sqlite table.
Sqlite creation string: "createdDate date time"
Storing parse date into table: "insert... parseObject.getCreatedAt()"
If i then use a SQLite browser to inspect the table, I see the date stored like this:
Sat Jun 15 15:44:52 PDT 2013
So going along with that, I wrote the following to convert it back into a DateTime object to give to parse as part of a query to get items that are newer than the last inserted in my table:
DateTimeFormatter format = DatetimeFormat.forPattern("yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'");
DateTime dt = formatter.parseDateTime(datahelper.getLastInsertDate(..));
The formatter is this way, because in Parse's databrowser, I can see dateTimes being stored like this:
2013-06-24T08:11:45.280Z
I get an ANR though, so I tried using the following formatter:
DateTimeFormatter format = DatetimeFormat.forPattern("EEE' 'MMM' 'dd' 'HH':'mm':'ss 'z'' 'YYYY");
DateTime dt = formatter.parseDateTime(datahelper.getLastInsertDate(..));
and I still get an ANR. The trace in eclipse shows the following:
Caused by: java.lang.IllegalArgumentException: invalid format: "Tue Jun 25 00:13:29 PDT 2013"
at org.joda.time.format.DateTimeFormatter.parseDateTime"
The second ANR trace shows:
Invalid format: "Tue Jun 25 00:13:29 PDT 2013" is malformed at "PDT
2013"
I've tried getting around that, as joda time does not parse "z" to PDT/PST, so I've put 'PDT' in my formatter to hopefully get it to work, but nothing seems to work.
Any ideas?
Edit 1: Using the accepted answer, I have a timezone formatting issue)
DateFormat originalFormat = new SimpleDateFormat("EEE MMM DDD HH:mm:ss z yyyy");
Date originaldate = originalFormat.parse(datahelper.getLastInsertdate);
Log.i("converted date: ", String.valueOf(originalDate);
Log.i("a real date: ", "String.valueOf(new Date(new Date().getTime)));
I get two outputs:
Fri Jan 25 15:14:11 PST 2013
Tue Jun 25 17:11:44 PDT 2013
why does the converted date show PST, and a standard Date shows PDT?
It seems to be a known problem Joda cannot parse Timezone names sadly. In the documentation before all the pattern syntaxes you will see this line:
The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported. All ASCII letters are reserved as pattern letters, which are defined as follows:
You can see that in the documentation link here
Now the solution to your answer can be found in this answer by #BalusC located here
Hope this helps.
I think that with SQLite, because the date type is somewhat broken, the best thing to do is to store the long that you get from Date.getTime() or related Joda methods.
When you get the long from the database, re-construct your date object (e.g. new Date(long)), and then format that.
Above all, remember that (IMHO) the only sensible way to store a date is in reference to UTC, which is what you get with Date.getTime() and new Date(long) : milliseconds since Jan 1, 1970 UTC.
Once you retrieve your date, format it with whatever timezone is appropriate.

Get time of different Time zones on selection of time from time picker

I have an issue of converting selected hours and minutes to different time zones of countries.
Supposing if i select 10 am in India then i want to know at 10 am in india what will be the time in USA/New york and Tokyo.and Vice versa.
Any help is appreciable...
Thank you
please find the sollution below :
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mma");
TimeZone timezone = TimeZone.getDefault();
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Date d = new Date();
sdf.setTimeZone(timezone);
String strtime = sdf.format(d);
Log.e("str time gmt ",strtime);
sdf.setTimeZone(utcTimeZone);
strtime = sdf.format(d);
Log.e("str time utc ",strtime);
i think this will solve your problem
You can probably use Joda Time - Java date and time API. You can get the DateTimeZone depending on the Canonical ID defined in the Joda Time,
DateTimeZone zone = DateTimeZone.forID("Asia/Kolkata");
Joda Time has a complete list of Canonical ID from where you can get TimeZone depending on the Canonical ID.
So, if you want to get the local time in New York at this very moment, you would do the following
// get current moment in default time zone
DateTime dt = new DateTime();
// translate to New York local time
DateTime dtNewYork = dt.withZone(DateTimeZone.forID("America/New_York"));
For getting more idea you can refer Changing TimeZone
Try using Joda-Time library
check the org.joda.time.DateTimeZone class
Here is the API documentation for the same.
you can also get it using , Here no external API is needed
DateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
TimeZone utc = TimeZone.getTimeZone("America/New_York");
System.out.println(utc.getID());
GregorianCalendar gc = new GregorianCalendar(utc);
Date now = gc.getTime();
System.out.println(format.format(now));
you can see more time zone on this Link
Output
America/New_York
December 29, 2012, 11:04 AM
If you don't know city name then you can also use it by Zone name as follow
DateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
TimeZone cst = TimeZone.getTimeZone("US/Eastern");
System.out.println(cst.getID());
GregorianCalendar gc = new GregorianCalendar(cst);
Date now = gc.getTime();
format.setTimeZone(cst);
System.out.println(format.format(now))
Output
US/Eastern
December 29, 2012, 12:38 AM
Not really sure about the solution I'm going to provide but I think you can try it. GMT (Greenwich Mean Time) is a standard. I think you can keep it as a base and calculate the desired time. GMT standard is easily available too.
For example: While installing an OS like Windows XP or Windows 7, we select the time from a drop down menu. My point is, keeping this as the base, you can find the difference between the time zones in NY-US and Tokyo-Japan or vice versa as you desire it.
Hope this helps.

How do I get the time in this format in android?

I am trying to get the present time in this format in an android app. time= "05:09pm 08/02/2011" Right now I am using Calendar c=Calendar.getInstance() and c.getTime() to get the time and its coming out as Tue Aug 23 02:34:25 PDT 2011.
Thanks
You need to use the DateFormat Class
Something like this will get you the current time in the format you desire.
DateFormat.format("hh:mmaa dd/MM/yyyy", System.currentTimeMillis());
Use a SimpleDateFormat.
Format should be like
SimpleDateFormat sdf = new SimpleDateFormat( "HH:mma dd/MM/yyyy" );
sdf.format( yourDate );
Regards,
Stéphane
There are many ways to do that in Android. You can use the SimpleDateFormat wich is a class for formatting and parsing dates. Formatting turns a Date into a String, and parsing turns a String into a Date. Or you can the class Formatter wich is low level but managing the localization is your responsibility.
You may find source code example on the Android javadoc on those classes

date format in android

I am stuck in one date format
i want my date should look like this, 18th Mar 2011
and it can be 1st, 2nd,3rd that means i want to resolve for all the aspects
Plz help me out for this ASAP
Thanks in advance to all.
I think you want to change date in a 18th, 2nd, 1st, 3rd dated way, if i am not wrong then you can use simpleDateFormat class to convert date in different formats.
Before using SimpleDateFormat, just refer the SDK documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html.
To have day number with nd, th, rd (i.e. 2nd, 4th, 3rd, etc.), you can use:
F - day of week in month (Number) - 2 (2nd Wed in July)
(given in the documentation).
For example using SimpleDateFormat:
String dateStr = "03/08/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
This is the term you are looking for: Quantity Strings (Plurals)
here is a link for it in the documentation:
Plurals
Here is a link with examples:
Examples
And One more:
Android Pluralization not working, need help
Hope this helps. so you just need to reformat the string with the date by using the examples provided and thats it.

Categories

Resources