I have a problem that I am having an Epoch date which is coming from Web Services, I want to display it in Human Readable format as, July 12, 2012, but my code always shows 16/01/1970 for any Epoch Date. I don't know where I am Wrong or How to convert Epoch Date to Date in Java. Please suggest me any solution regarding the same.
Code:
Date.setText(new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(Long.parseLong(newsDetail.getDate_Posted()))));
Thanks in advance.
I have a hunch that Long.parseLong(newsDetail.getDate_Posted()) is returning seconds from epoch not milliseconds.
If you always see 16/01/1970, then Long.parseLong(newsDetail.getDate_Posted()) is between 1296000000 and 1382400000. It turns out that if I multiple these minimum and maximum number by 1000, I see dates between 26/1/2011 and 21/10/2013. I'm guessing that the date you are expecting falls in this new range.
Try using:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date.setText(sdf.format(new Date(Long.parseLong(newsDetail.getDate_Posted()) * 1000)));
Related
I currently work on a double value that represent the total consumed time
for example, I have a 260 that means 260 second is consumed
To display to user, I would like to format it
for example , it should be something like 0year,0month,0day,1hr,2min,30sec
But I found the SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); is not quite suit my case (seems the "h" in "hr" is conflicted with the hour symbol?)
So , how to change the HH:mm:ss to the case mentioned above?
Thanks for helping
DateFormat is useful to format dates, not an absolute value of time.
To achieve your goal, you can take a look to Formatter
Hope this sample helps you:
String total_consumed_time = String.format("%01d year, %01d month, %01d day, %01d hr, %01d min, %01d sec", time_year, time_month, time_day, time_hour, time_min, time_seg);
I didn't try that code, but I use similar workaround with an absolute time in milliseconds:
long time = 260000; // time in mseg
long time_hour = TimeUnit.MILLISECONDS.toHours(time);
time -= TimeUnit.HOURS.toMillis(time_hour);
long time_min = TimeUnit.MILLISECONDS.toMinutes(time);
time -= TimeUnit.MINUTES.toMillis(time_min);
long time_seg = TimeUnit.MILLISECONDS.toSeconds(time);
String total_time = String.format("%02d:%02d:%02d", time_hour, time_min, time_seg);
With a result of "00:04:20" (4 minutes and 20 seconds).
Accepted answer is in most cases okay for solving your problem, but gives wrong reason why not to use the class SimpleDateFormat. This format class is well suited for objects of type java.util.Date (which are kind of unix timestamps in milliseconds hence absolute value of time, NOT dates). In order to treat letters like "hr" as literals you need to escape them. Example code:
// create timestamp
java.util.Date jud = new java.util.Date(260 * 1000); // milliseconds
// create format for timestamp
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy'year',M'month',d'day',H'hr',m'min',s'sec'");
// otherwise you will get extra offset time (example: in England +1 hour DST)
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
// output: 1970year,1month,1day,0hr,4min,20sec
String formatted = sdf.format(jud);
System.out.println(formatted);
Even with the applied and tricky time zone correction in code you face the problem that you have an output for the year 1970, a point in time. Hereby you can see that SimpleDateFormat does format timestamps well (absolute values in time) but NOT durations (amount resp. length of time). This semantic problem can also not be solved by the approach to use java.util.Formatter as soon as the input increases the day limit of 86400 seconds.
Old JDK and Android don't offer a built-in solution for evaluating time differences expressed in years, months and days. Java 8 does offer (limited) support with new API (class 'Period' only for date part, not time part). External libraries like JodaTime or my own one (actually only as alpha-version) give more support. JodaTime even offers a special PeriodFormatter which is ideal for solving your problem.
I am having following code to convert milliseconds to Android Date object.
Date dateObj = new Date(milli);
But problem is that my milliseconds value is having GMT value added in it before i pass it to Date class, add when i print this date object i can see that date object is again adding GMT value in the milliseconds value and because of that my date is displayed as wrong.
So how can i generate Date object with out considering GMT value in it.
For example my milliseconds are 1385569800000 which is getting printed as below:
Wed, 27 Nov 2013 22:00:00 --> +5.30
But the current value of this time stamp without adding GMT is:
Wed, 27 Nov 2013 16:30:00
*UPDAE*
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
So basically i want to create Date object which has proper date time value in it with out adding extra GMT time added in it.
A Date is always in UTC. No need to change that.
When printing the date value, use SimpleDateFormat and call setTimeZone() on it before formatting the output string.
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
TimerTask is just a task and not its scheduling. Timer accepts a Date object for scheduling. The Date is in UTC there as well.
try my code if you a
long currentTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
currentTime -= offsetInMillis;
Date date = new Date(currentTime);
it is work for me
You can try with joda-time API.
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.
http://joda-time.sourceforge.net/key_instant.html
A Date object simply represents a moment in time. Imagine you're on the phone to someone on a different continent, and you say "3...2...1...NOW!". That "NOW" is the same moment for both of you, even though for one person it's 9am and for the other it's 4pm.
You're creating a Date representing the moment 1385569800000 milliseconds after the Java epoch (the beginning of 1970, GMT). That is your "NOW", and it's fixed and unchanging. What it looks like converted into text, however, depends on which timezone you want to display it for. Java defaults to using GMT, which would be right if you were in Britain during the winter, but for (I'm guessing) India you want it in a different time zone. Laalto's answer shows you how to do that.
here is the code,that worked like charm for me:
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
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
I am trying to get the date in this format "time":"05:09pm 08/02/2011". What I have so far is
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mmaa MM/dd/yyyy");
holder.put("time", sdf.format(c.getTime()));
and this is what comes out.
"time":"21:28PM 08\/26\/2011"
Why is this happening and what can I do to fix it? Thanks
I suspect you want a format of
hh:mmaa MM/dd/yyyy
As hh is the 12-hour format in the range 01-12. I find that's typically how humans represent 12-hour values. Of course if you want 00-11, use KK instead as suggested by MByD.
(Quite what the rationale is for the capitalized form being the 24-hour version of HH, but the lower case form being the 24-hour version of kk, I don't know...)
If you were worried by the backslashes in the output, I suspect that's just JSON-escaping. I'm surprised it's necessary for forward-slashes, but it shouldn't do any harm.
Try changing HH:mmaa MM/dd/yyyy to KK:mmaa MM/dd/yyyy. (KK is hour in am/pm, 0-11)
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.