json to human readable date date shows one less than actual date - android

I convert json date to human readable date but it shows less one then actual date. I
used this code to convert it:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Long timeInMillis = Long.valueOf(AttendanceModelList.get(position).getEmpdate());
calendar.setTimeInMillis(timeInMillis);
Date date=new Date(timeInMillis);
viewHolder.textemployeedate.setText(df.format(date));
Please help

You say as summary:
Your calendar date is one day less than expected when you try to
interprete a global timestamp of type java.util.Date as calendar
date.
This phenomenon can happen due to timezone effects or midnight change. Before viewing the technical solution, you have to ask yourself:
What is your default (system) timezone using TimeZone.getDefault()?
Do you run your code on a server which has not the expected timezone?
In which timezone do you wish to view the calendar date? (the timezone associated with your expected "actual" date)
How to specify the timezone?
java.util.Date d = ...; // from your JSON-timeInMillis?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tz = "Asia/Kolkata"; // or any other valid tz id
sdf.setTimeZone(TimeZone.getTimeZone(tz));
System.out.println(sdf.format(d));

Related

Year getting converted 1970 when using date in currentTimeMillis()

I am working on an app where I am saving date in currentTimeMillis() in sqlite database and when I read it I try to convert it to readable format. But date is always being converted to Jan 01, 1970.
Following is my code of Saving it to sqlite database:
long date = System.currentTimeMillis();
I then try to convert it to readable format in following way:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(attendance.getPicDate());
Date resultdate = new Date(attendance.getPicDate());
System.out.println(sdf.format(resultdate));
I am getting the following output:
Jan 17,1970 19:36
Edit1: attendance.getPicDate() value = 1432678159
Edit2: when I try to save value it is 1500378235812 but when I read it, it becomes above, My column in table is Integer.
1432678159 does convert to Jan 17,1970 19:36.
Try checking attendance.setPicDate().
I found my mistake I was using cursor.getInt(2) to fetch date from database but now I have corrected it to cursor.getLong(2) and it is working great now.
I suggest, you should save date and time in string format in database.
Calendar calendar=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String currentTime=sdf.format(calendar.getTime());
save that "currentTime" in your database.

Getting date from another timezone

How can I change the timezone, when I use this code to get the date?
Calendar c = Calendar.getInstance();
String date = org.apache.http.impl.cookie.DateUtils.formatDate(c.getTime());
This returns me the date in my timezone (GMT+00:00) but I need the CET time.
Use the setTimeZone method on your calendar to set the time zone to CET.
c.setTimeZone( "Europe/Amsterdam" );

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 :).

Retrieved time not converted to current time zone

I have to convert the time which i get as input and convert to current time zone which had been set in device and display.
I tried some extent but the time not changed while converting.Have posted my code..
String dateandtime="3/14/2013 2:38am" //EDT time
Calendar cal = Calendar.getInstance();
TimeZone z = cal.getTimeZone();
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy hh:mma");
format1.setTimeZone(z);
cal.setTime(format1.parse(dateandtime));
The value of "dateandtime" wil be EDT timezone and should convert to any of current time zone.. I receive the same date and time as output..

Calendar not returning GMT

I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.

Categories

Resources