Last active
July 22, 2019 08:58
-
-
Save pavelsust/45e5f8d049f9f5d9fc77f31b82231856 to your computer and use it in GitHub Desktop.
Current date and Time Convert to GMT time and compare
This file contains hidden or 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
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 MMM 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 MMM 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