Reputation: 31
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();
Upvotes: 2
Views: 1855
Reputation: 338326
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()
Upvotes: 0
Reputation: 7209
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());
}
Upvotes: 1
Reputation: 4117
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;
Upvotes: 1