Skip to content

Instantly share code, notes, and snippets.

@pavelsust
Created August 26, 2019 05:42
Show Gist options
  • Save pavelsust/c95316d8065cab4e060de30244c9fdd6 to your computer and use it in GitHub Desktop.
Save pavelsust/c95316d8065cab4e060de30244c9fdd6 to your computer and use it in GitHub Desktop.
Convert any date to GMT and compare two date
package packagename;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Datetest {
public static String DATE_FORMAT = "yyyy-MM-dd hh:mm a zzz";
public static void main(String[] args) {
System.out.println("final date to GMT " + finalDate());
System.out.println("current time: " + getCurrentDate() + "\n");
System.out.println("result: " + compareTwoDate());
}
public static String finalDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
String currentDate = sdf.format(new Date());
SimpleDateFormat zoDateFormat = new SimpleDateFormat("zzz");
String currentZone = zoDateFormat.format(new Date());
return currentDate + "3:00 PM " + "GMT+6:00";
}
public static String getCurrentDate() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Date date = new Date();
sdf.setTimeZone(TimeZone.getTimeZone("GMT+6:00"));
return sdf.format(date);
}
public static String convertToGMTDate(String dateString) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Date date = new Date();
try {
date = sdf.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sdf.setTimeZone(TimeZone.getTimeZone("GMT+6:00"));
return sdf.format(date);
}
public static boolean compareTwoDate() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Date currentDateTime = null;
Date setDateTime = null;
try {
currentDateTime = sdf.parse(getCurrentDate());
setDateTime = sdf.parse(convertToGMTDate(finalDate()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (currentDateTime.before(setDateTime)) {
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment