import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
public class ConvertDateTime {
public static TimeZone tzGMT = TimeZone.getTimeZone("GMT0");
public static TimeZone tzEL = TimeZone.getTimeZone("Europe/London");
/*
*converts the system time to GMT
*/
public static Timestamp currentTimeInGMT() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
DateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone firstTime = TimeZone.getTimeZone(dateFormat.format(date));
firstFormat.setTimeZone(firstTime);
//secondFormat.setTimeZone(secondTime);
System.out.println("Current date and time: "+dateFormat.format(date));
System.out.println("Time in GMT: ");
// System.out.println("Time in GMT: "+ firstFormat.format(date) +" GMT");
return Timestamp.valueOf(firstFormat.format(date));
}
/*
*converts the system time to BST
*/
public static Timestamp currentTimeInBST() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
DateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone firstTime = TimeZone.getTimeZone(dateFormat.format(date));
TimeZone secondTime = tzEL;
firstFormat.setTimeZone(firstTime);
secondFormat.setTimeZone(secondTime);
System.out.println("Current date and time: "+dateFormat.format(date));
System.out.println("Time in GMT: " + firstFormat.format(date)+" GMT");
System.out.println("Time in BST: "+ secondFormat.format(date)+" BST");
return Timestamp.valueOf(secondFormat.format(date));
}
/*
*converts the time BST to GMT
*/
public static synchronized Timestamp BSTtoGMT(Date bstTime){
String bstStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(bstTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(tzEL);
Date gmtDate=null;
try {
gmtDate =sdf.parse(bstStr);
//System.out.println(gmtDate);
} catch (ParseException e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(gmtDate.getTime());
return ts;
}
/*
*converts the time GMT to BST
*/
public static synchronized Timestamp GMTtoBST(Timestamp gmtTime){
String gmtStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(gmtTime);
// SORT date is always in BST
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(tzGMT);
Date bstDate=null;
try {
bstDate = sdf.parse(gmtStr);
//System.out.println(bstDate);
} catch (ParseException e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(bstDate.getTime());
return ts;
}
}