0

hello my bro & sis i am new in android, currently i am working in android database site. my application user used app from the multiple country, So my problem is how can i insert current time of every user from different locale wise when user perform any database transaction, even if user's android mobile device current time is not proper set yet i want to insert current time in database of his region.

Note: currently i am using MySQL database

Now i am insert current time of user i this way

String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).concat(" 00:00:00");

but this is working for only one locale which is default time zone is set. So, Please somebody help me how to resolve my problem.

Mansukh Ahir
  • 3,373
  • 5
  • 40
  • 66

2 Answers2

0

For saving time inside a database, the best and most country compatible way is to use the time as Long. For example, create a calendar instance:

   Calendar cal = Calendar.getInstance();
   Long timeToSave = cal.getTimeInMillis();

Then You have to save this Long value inside the database. With this value, You can set all Dateformats for all time zones, also AM/PM values and so on.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

I'd recommend to keep the DateTime type in SQL as Long. Please try this code:

Long now = System.currentTimeMillis();

If you ever need to, you will be able to convert the Long value to string for the current locale:

DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Session.currentLocale);
String date = dateFormat.format(yourStoredDateAsLong);

or

String date = dateFormat.format(System.currentTimeMillis());
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Dobry
  • 161
  • 5