Created
February 1, 2021 13:39
-
-
Save farhanfahim/151997ca3096006aa5b2529bf0c34902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.testprojectbroadcast; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.TimeZone; | |
public class MainActivity extends AppCompatActivity { | |
static int gmt = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//current date | |
Date date = new Date(); | |
String currentDateInString = date.toString(); | |
// get gmt value according to country | |
String[] arrOfStr = currentDateInString.split("GMT"); | |
String[] arrOfStr1 = arrOfStr[1].split(":"); | |
gmt = Integer.parseInt(arrOfStr1[0]); | |
try { | |
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm:ss"); | |
String newDate = "/*YOUR SERVER DATE TIME HERE*/"; | |
Date date1 = formatter.parse(newDate); | |
Date localToGtm = dateToUTC(date1); | |
Log.d("PRINT_GMT", "Current time: " + date.toString()); | |
Log.d("PRINT_GMT", "Server time: " + newDate); | |
Log.d("PRINT_GMT", "Current time in GMT: " + date.toString()); | |
Log.d("PRINT_GMT", "Server time in GMT: " + localToGtm.toString()); | |
Log.d("PRINT_GMT", "Converted date after GMT value added in current time: " + formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "GMT")); | |
Log.d("PRINT_GMT", "Converted date after GMT value added in server time: " + formatDateToString(localToGtm, "dd MMM yyyy hh:mm:ss a", null)); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static Date dateToUTC(Date date) { | |
return new Date(date.getTime() - Calendar.getInstance().getTimeZone().getOffset(date.getTime())); | |
} | |
public static String formatDateToString(Date date, | |
String format, | |
String timeZone) { | |
// null check | |
if (date == null) return null; | |
// create SimpleDateFormat object with input format | |
SimpleDateFormat sdf = new SimpleDateFormat(format); | |
// default system timezone if passed null or empty | |
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) { | |
timeZone = Calendar.getInstance().getTimeZone().getID(); | |
} | |
// set timezone to SimpleDateFormat | |
sdf.setTimeZone(TimeZone.getTimeZone(timeZone)); | |
// return Date in required format with timezone as String | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
cal.add(Calendar.HOUR, gmt); | |
return sdf.format(cal.getTime()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment