2

I want to get the current time according to current time zone even I change my current time zone from my phone and change my time but still, I should be able to get the current time and date. How to get that?

Time currentTime = new Time();
currentTime.setToNow();

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();

Date date = cal.getTime();
strDate = date.toString();
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Aditya
  • 31
  • 6

3 Answers3

1

Use this method to get local time

   public static Date getLocalDate(String timeStamp) {
        int index = timeStamp.indexOf("+");
        String dateTime;
        if (index > 0) {
            timeStamp = timeStamp.substring(0, index);
        }

        int indexGMT = timeStamp.indexOf("GMT");
        int indexPlus = timeStamp.indexOf("+");
        if (indexGMT > 0) {
            dateTime = timeStamp.substring(0, indexGMT);
        } else if (indexPlus > 0) {
            dateTime = timeStamp.substring(0, indexPlus);
        } else {
            dateTime = timeStamp;
        }
        SimpleDateFormat sdfgmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        SimpleDateFormat sdfmad = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdfmad.setTimeZone(TimeZone.getDefault());
        Date gmtDate;
        Date localDate = null;
        try {
            gmtDate = sdfgmt.parse(dateTime);
            localDate = sdfmad.parse(sdfmad.format(gmtDate));
        } catch (ParseException e) {

        }
        return localDate;
    }

To get current UNIX timestamp, you can use the below code

 long unixTime = System.currentTimeMillis() / 1000L;
Rishabh Sagar
  • 3,877
  • 3
  • 15
  • 24
1

try this

KOTLIN

fun getDateTimeString( ): String {
            val calendar = Calendar.getInstance()
            calendar.timeZone= TimeZone.getDefault()
            calendar.timeInMillis = Date().time
            return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)

    }

JAVA

private String getDateTimeString(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getDefault());
        calendar.setTimeInMillis(new Date().getTime());
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.getTime());
    }

Get time form Google : How to get current time from Google for Android?

try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            String dateStr = response.getFirstHeader("Date").getValue();
            //Here I do something with the Date String
            System.out.println(dateStr);

        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    }catch (ClientProtocolException e) {
        Log.d("Response", e.getMessage());
    }catch (IOException e) {
        Log.d("Response", e.getMessage());
    }
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
  • can you please give me java code not kotlin @Lakhwinder – Aditya Aug 12 '19 at 05:57
  • i changed my time and date from settings and then it gave me the changed date and time not today's date and time @Lakhwinder – Aditya Aug 12 '19 at 06:15
  • Obviously, it will give you the time basis on your device's current time, what do you actually required? – Lakhwinder Singh Aug 12 '19 at 06:17
  • I want to get the current time according to current time zone even I change my current time zone from my phone and change my time or date but still, I should be able to get the current time and date. How to get that? – Aditya Aug 12 '19 at 06:20
  • is that be helpful, if i let you know that time is not set to Automatic, then you can ask the user to set time to automatic and it will give you the correct time always – Lakhwinder Singh Aug 12 '19 at 06:22
0

java.time

Use modern java.time classes.

Get the current default time zone of the JVM.

ZoneId z = ZoneId.systemDefault() ; 

Or ask the user.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;

Get the current moment as seen in the wall-clock time used by the people of that region.

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Extract the date portion.

LocalDate ld = zdt.toLocalDate() ;

Extract the time of day.

LocalTime lt = zdt.toLocalTime() ; 

If you want an update, if you want to gather the current moment again using whatever zone might now be current, simply repeat the steps above. The java.time objects are immutable, and do not auto-update; you must ask for new objects with fresh data.

ZonedDateTime.now( ZoneId.systmDefault() ).toLocalDate()
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154