Get current date as String warning deprecated toGmtString() issue - android

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

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

Converting seconds to date time String

I have seconds from epoch time and want to convert it to Day-Month-Year HH:MM
I have tried following but it gives me wrong value.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
Above code is not working properly am i doing anything wrong here.
For example if seconds = 1299671538
then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
For example if seconds = 1299671538 then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
You have integer overflow. Just use the following (notice "L" after 1000 constant):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000L);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
or better use SimpleDateFormat class:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm");
String dateString = formatter.format(new Date(seconds * 1000L));
this will give you the following date string for your original seconds input:
Wednesday, March 9, 2011 13:52
You need to use
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
instead of
Calendar cal = Calendar.getInstance();
because UTC time from seconds depends on Timezone.
You don't need a calendar in this case, you can simply use the constructor new Date(1000 * seconds)
Then use a SimpleDateFormat to create a String to display it.
For a full explanation on using SimpleDateFormat go here.
The answer to this question though is that you need to use long values instead of ints.
new Date(1299674566000l)
If you don't believe me, run this:
int secondsInt = 1299674566;
System.out.println(new Date(secondsInt *1000));
long secondsLong = 1299674566;
System.out.println(new Date(secondsLong *1000));
I can confirm that answer from #Idolon is working fine, simple snippet is below...
long created = 1300563523;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
String dateString = formatter.format(new Date(created * 1000L));
using SimpleDateFormat is better way, change the format as you need.
Won't it work wihtout Calendar, like below? Haven't run this piece of code, but guess it should work .
CharSequence theDate = DateFormat.format("Day-Month-Year HH:MM", objDate);

Categories

Resources