0

My web server is in Us i am posting comment in India. I am storing comment time as server time. Instead of showing just now it shows 6hr before means there is difference of 6 hour between actual comment time and server time.

I am using Spring-MVC on server side and cassandra as DB Here is how i am taking time of a comment:

long commentTime = new Date().getTime()

here how i am displaying date

SimpleDateFormat sdf  = new SimpleDateFormat("YYYY MM dd hh:mm:ss a");
lk=Long.parseLong("date in long as string");
myPageList.setCreatedOn(sdf.format(new Date(lk)));

What are the way to sync time between client and server ?

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • oh sorry i added how i am displaying it – Manish Kumar Oct 16 '14 at 20:20
  • Calendar.getInstance(locale) : You can get local from browser and get instance with that locale can make that trick – Gurkan İlleez Oct 16 '14 at 20:24
  • so if i post `just now` in India, In US it will show `just now` or `3-4 hour before` – Manish Kumar Oct 16 '14 at 20:26
  • Date-time work and client-server work are tricky subjects. Seems like you don't quite know enough to ask the right questions. You should do some searching in StackOverflow for study and orientation. Try terms like "java date" "joda" and "database date". Start [here](http://stackoverflow.com/q/2532729/642706), [here](http://stackoverflow.com/q/24015451/642706), [here](http://stackoverflow.com/q/25294648/642706), [here](http://stackoverflow.com/q/25227357/642706). – Basil Bourque Oct 17 '14 at 00:45

1 Answers1

2

Start using a time API which supports time zone conversions (e.g. Java 8's java.time or Joda-Time). Don't persist a local date in the data base. Store the point of time in the database (i.e. with a time zone).

On the presentation side you will have to know the client's local time zone. One method is storing that information in a user profile (if you have authenticated users). Another method might involve some Javascript magic. With the local time zone and a proper time API you can easily present the stored point of time in the local time.

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67