shivpal jodha
shivpal jodha

Reputation: 102

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

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

Upvotes: 0

Views: 210

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44071

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

Upvotes: 1

Related Questions