2

I want local date and time of my android phone. I have tried the following code :

Calendar calendar=Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("IST"));
String output=calendar.getTime()+"";

And i am getting output : Sat Mar 26 11:15:38 GMT+00:00 2016 and this is wrong actual date and time was "26th March 2016" and time was "16:46", i am getting date proper but time wrong and even got GMT timezone instead IST timezone. Is that because of Timezone if yes please let me know other solutions. Thanx in advance

Rahul Verma
  • 514
  • 2
  • 8
  • 16
  • Android doesn't support the three-letter time zone IDs, other than UTC and GMT. Have a look at [this post](http://stackoverflow.com/questions/30416343/how-to-get-ist-time-zone-in-android). – Mike M. Mar 26 '16 at 11:28
  • ok. is there anyway i can convert this GMT+00:00 into GMT+05:30?? – Rahul Verma Mar 26 '16 at 11:31
  • Did you check the post I linked? – Mike M. Mar 26 '16 at 11:33
  • Yes and i am trying it – Rahul Verma Mar 26 '16 at 11:35
  • If you don't want to do it that way, have a look at [the docs](http://developer.android.com/reference/java/util/TimeZone.html#getTimeZone(java.lang.String)). It explains how to do it with raw offsets. – Mike M. Mar 26 '16 at 11:36
  • Thanx Mike it worked :) – Rahul Verma Mar 26 '16 at 11:39
  • No problem. You should be able to mark that post as helpful to close out your question now. Glad you got it working. Cheers! – Mike M. Mar 26 '16 at 11:40
  • Kind of related, but there still seems to be a lot of confusion around "time in milliseconds". See my explanation on that -- https://stackoverflow.com/a/57220021/2102748. I know I stumbled upon this first, and then looked up time in millis later on. – milosmns Jul 26 '19 at 12:32

4 Answers4

0

I Use this -

public String getTime(){
       return new SimpleDateFormat("dd/MM/yy HH:mm:ss").format(new Date());

You can also try

return DateFormat.getDateTimeInstance().format(new Date());

For different patterns and more details, please look at the android documentation for date and time

Imdad
  • 683
  • 1
  • 9
  • 27
  • Thanx for replying and I did and i am getting date perfectly but time value wrong. I got "26/03/16 11:25:00" instead of "26/03/16 16:58:00" – Rahul Verma Mar 26 '16 at 11:28
  • Try this- Time today = new Time(Time.getCurrentTimezone()); today.setToNow(); If even this comes wrong, please check your phone for Time setting – Imdad Mar 26 '16 at 11:33
0

Try it..

 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
Nandkishor mewara
  • 2,552
  • 16
  • 29
0

This code works best for me:-

      Calendar cal = Calendar.getInstance();  
        hours = cal.get( Calendar.HOUR_OF_DAY );  
        if ( hours > 12 ) hours -= 12;  
        minutes = cal.get( Calendar.MINUTE );  
        seconds = cal.get( Calendar.SECOND );  

        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  
        Date date = cal.getTime();  
        timeString = formatter.format( date );  
Ankesh kumar Jaisansaria
  • 1,563
  • 4
  • 26
  • 44
0
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
     Locale.getDefault());

Use this to set the variable to your time zone or you can specify Continent/Country to be more specific forexample:

dateFormat.timeZone = TimeZone.getTimeZone("Asia/India");
Date c = Calendar.getInstance().getTime();
String currentDate = ddateFormat.format(c);    //You can use this as your output date
DANIEL SSEJJEMBA
  • 342
  • 5
  • 15