android date toString - android

I"m following a tutorial where they have this line:
int callDate = c.getInt(dateColumn);
Next, they pass the callDate into the Date object to get a readable format.
But, I think the example may be old, because I get a compile error.
String myDate = DateUtils.dateString(callDate).toString();;
So, I'm looking at the API. how do I do it.
http://developer.android.com/reference/java/util/Date.html

String myString = DateFormat.getDateInstance().format(new Date());
or
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
Look at DateFormat and SimpleDateFormat.

You might want to use SimpleDateFormat for that

If I have understood your question correctly, you need to get the date in readable format.In that case you already have the answer you just need to read the link http://developer.android.com/reference/java/util/Date.html carefully for finding the answer. Let me provide you few hint for the same, see for the constructors on this link for initializing the date of your choice. The methods given helps you perform the operations on it. To answer your question specifically refer below snippet.
Date d1=new Date(); //Initializes this Date instance to the current time.
String date= d1.toString(); //this String var date can be used for printing date in readable format
the format of string will be "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999".

See the below link. It will definitely help.
Simple java date conversion

the problem is that you are getting from a Integer
int callDate = c.getInt(dateColumn);
the date is a Long type and you're losing precision on this conversion.
try changing to: long callDate = c.getLong(dateColumn);

Related

How can I compare a string (of a given format) to current date?

I want to compare a date string (that I get form a viewHolder) with this format "dd-MM-yyyy HH:mm" to current date and time but I keep getting java.lang.IllegalArgumentException: Cannot format given Object as a Date. This is how I tried:
SimpleDateFormat dateAndTimeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.ROOT);
LocalDate localDate = LocalDate.now();
String currentDateAndTime = dateAndTimeFormat.format(localDate.toString());
if(viewHolder.time.getText().toString().equals(currentDateAndTime)){
//irrelevant
}
SimpleDataFormat.format(...) takes a Date object as its first parameter. You're passing in a String because you're calling toString() on it. Try this:
SimpleDateFormat dateAndTimeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.ROOT);
String currentDateAndTime = dateAndTimeFormat.format(new Date());
if (viewHolder.time.getText().toString().equals(currentDateAndTime)) {
//irrelevant
}
Go all-in on java.time, the modern Java dat and time API.
DateTimeFormatter dateAndTimeFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm", Locale.ROOT);
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault());
String currentDateAndTime = zdt.format(dateAndTimeFormat);
System.out.println(currentDateAndTime);
When I ran the code just now using Pacific/Honolulu time zone as my default, the output was:
01-05-2020 08:47
What went wrong in your code?
Two things were wrong:
A LocalDate is just a date without time of day, so there is no way you can format it into a string that includes hour of day and minute. Instead use ZonedDateTime, OffsetDateTime or LocalDateTime.
It’s good to use LocalDate or another class from java.time, the modern Java date and time API. The modern classes and the outdated SimpleDateFormat don’t work together. To format a date and/or time object from java.time you need to use the modern DateTimeFormatter class. Which is good because SimpleDateFormat is a notoriously troublesome class that you should not want to use under any circumstances. What tends to fool us is that your code actually compiled, which gives the impression that it should work. It was never meant to. SimpleDateFormat inherits the format(Object) method from Format, a general superclass for formatting many kinds of objects, which is why the argument is declared to be Object, not Date (another outdated class).

How can I save string type into android SQLite datetime field

I've got following string "2013-04-30T00:55:25.855-07:00" from Google blogger feed. I try to save this string to SQLite datetime field as following command
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
initialValues.put(newsdatemodified, dateFormat.format(datemodified));
But I found that data cannot insert into sqlite table and no encounter any errors. Any solution will be appreciated.
Update
When I've tried to use following coding
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date1 = (Date) dateFormat.parse(entry.updated);
but following this error
05-02 09:56:05.383: E/AndroidRuntime(31634): Caused by: java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
Your format is wrong, it needs to me yyyy-MM-dd'T'HH:mm:ss.SSSZ
as you can see the format you had does not match what the incoming string format was
It seems that issue is with the format. May be, you can try using yyyy-MM-dd'T'HH:mm:ss.SSSZ. SSS stands for fractional seconds and Z stands for time zone.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Hope it helps else please comment. You can see other formats and try to work around with them by seeing here.

MediaStore.Video.Media.DATE_MODIFIED format

Can anyone show me how to format the MediaStore.Video.Media.DATE_MODIFIED to the human readable date format. I read in the docs that the time is in seconds. So i simply multiply it by 1000 and use the SimpleDateFormat . Here is my code
Date d=new Date(mills*1000);
DateFormat df=new SimpleDateFormat("dd-mm-yyyy");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(d);
This returns a String 25-35-2012 that off course is not correct. Any help with this ?
Kind Regards
you should use capital M, m refers to minute in hour while M refer to Month in year.
see this for further info

Want to print date&time together in one text field in dd/mm/yyyy hh:mm format in android

I'm a newbie to android and struggling to achieve this.
I want to select date and time and also wants to add the selected date and time in one textfield with this format dd/mm/yyyy hh:mm.
I searched on this but couldn't find what I want to achieve.
any guidelines in terms of code or link would be appreciated.
Thanks
you can use SimpleDateFormat by which you can format the Date.
String format = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format);
System.out.println(sdf.format(yourdate));
use this you can your desired format
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
like this May 17, 2012 12:03:09 PM
If you need to make Custom Date and Time Picker then you can see how in this they have make.Android Date Slider

android timestamp

hi i have a small question please i am new to android and have a date and time stamp
which looks like this yyyy-mm-dd hh:mm:ss
and i want to insert it into an sqlite table then read it back and compare it to the current time
any suggestions or examples
i found "SimpleDateFormat" but was not sure how to use it....?
thanks a lot
Have you considered using Calendar.getInstance.getTimeInMillis()? It's just the long representation of a date in milliseconds since Jan. 1, 1970. Great thing is your data is being stored in a format agnostic way. Just when you need to display it use SimpleDateFormat however you'd like.
Here is what you would do:
String myDate = new String("your date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(myDate);
date.getTime(); //fetch the time as milliseconds from Jan 1, 1970
Try use
System.currentTimeMillis();
its undepend from Calender.

Categories

Resources