Get user friendly date android - 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

Related

How Get Current time in English in Android?

I want to get current time specifically in English to save it in Database,
to get Current time i use function
private String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
return (String) DateFormat.format(CURRENT_TIME_FORMAT, Calendar.getInstance().getTime());
}
but when i set Locale to different language it gives me current time in that language.
for example if i set
conf.setLocale(new Locale("mr"));
it gives me date in marathi. I specifically want it in English. How to do it.
And Also how to change the language of Date once it is saved.I mean if I have saved date in English and while display i want that date to be shown in some other language, how to do it.?
As ADM suggested. new function that worked
public String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
SimpleDateFormat dateFormat = new SimpleDateFormat(CURRENT_TIME_FORMAT, Locale.ENGLISH);
return dateFormat.format(Calendar.getInstance().getTime());
}
So now it always returns Date in English
This should fix your issue! Try any of these
here is a simple tutorial
For java.util.Date, just create a new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
log.d(dateFormat.format(date)); //2016/11/16 12:08:43
For java.util.Calendar, uses Calendar.getInstance()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
log.d(dateFormat.format(cal)); //2016/11/16 12:08:43
For java.time.LocalDateTime, uses LocalDateTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
log.d(dtf.format(now)); //2016/11/16 12:08:43
For java.time.LocalDate, uses LocalDate.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
log.d(dtf.format(localDate)); //2016/11/16

How to get Current Date and time of Dubai in Android?

I am getting current date of the device, but i want the date and time of city Dubai, Any one please help me in doing this.when the activity called the date and time should be displayed,
Any help will be appriciated. thank you..
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = format.parse("2011-03-01 15:10:37");
// => This time is in the user phone timezone, you will maybe need to turn it in UTC!
TimeZone tz = TimeZone.getTimeZone("Asia/Dubai");
format.setTimeZone(tz);
String result = format.format(parsed);
You can use TimeZone to pass to the SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Dubai"));

Data use only hour minute seconte

Im working on android and parse some XML file
I get some date with this
DateFormat h = new SimpleDateFormat ("hh:mm:ss", Locale.FRANCE);
DateFormat d = new SimpleDateFormat ("yyyy-M-dd'T'hh:mm:ss", Locale.FRANCE);
Date hdebut = h.parse(maString);
Probleme is Date is for Date not hour so it give me a 1 january 1970(start of timestamp right?) a the correct our so i can't compare by using
Date now new Date();
now.after(hdebut);
i have some method to getHours or month but they are decrepetead so i don't know if i can use them or if wa have a better way no to do it
Any idea?
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
Try this. There are a lot of constants you can use with Calendar class.
Date is deprecated. You should be using Calendar instead.
Calendar provides working after and before methods as well and should work with pretty much any date you'll get to use :).

Get current date as String warning deprecated toGmtString() issue

I want to get the current date day/month/year min:sec in Android so I have used the follow code
String data="";
Calendar c = Calendar.getInstance();
data=c.getTime().toGMTString();
But Eclipse notify me that the method .toGMTString(); is deprecated.
How could I get the current date as formatted String avoiding the use of this deprecated method?
From the Android documentation:
This method is deprecated.
use DateFormat
Since the GMT string is represented as 22 Jun 1999 13:02:00 GMT, we can use SimpleDateFormat (subclass of the abstract DateFormat) like so:
SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String asGmt = df.format(c.getTime()) + " GMT";
Might want to double-check that format, but this'll get you started. Have a look at this IDEOne code.
The method toGMTString() from the type Date is deprecated.
you can check this for different types of date formations.
In your case use
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/Oct/2012 08:36:52
If you want to print month number instead of month name
use
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/10/2012 08:36:52

Android date format using SimpleDate

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.

Categories

Resources