package JavaUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
/**
* This class contains utility methods to deal with dates.
*
*/
public class DateTimeUtils
{
public static final String YYYYMMDD = "yyyyMMdd";
public static final String BACKLASH_MM_DD_YYYY = "MM/dd/yyyy";
public static final String DASH_MMDDYYYY = "yyyy-MM-dd";
public static final String TIME_STAMP = "yyyy-MM-dd'T'hh:mm:ss:SSSS";
/**
* Returns current date and time depending on format string passed in
*/
public static String formatedCurrentDateTime(String formatterString)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(formatterString);
Date a_oCurrentTime = new Date();
return a_oFormatter.format(a_oCurrentTime);
}
/**
* Returns date and time depending on format string and date passed in
*/
public static String formatedDateTime(String formatterString, Date date)
{
String sformat = "";
if (date != null)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(formatterString);
sformat = a_oFormatter.format(date);
}
return sformat;
}
/**
* Returns a Date object from a simple date format + the date
*/
public static Date getDateFromDateFormat(String inFormatter, String date)
{
Date oDate = null;
if (date != null)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(inFormatter);
try
{
oDate = a_oFormatter.parse(date);
}
catch (ParseException ex)
{
// UtilsTrace.catching(DateTimeUtils.class, "getDateFromDateFormat", ex);
}
}
return oDate;
}
/**
* Converts one date format to another
*/
public static String convertDateTime(String inFormatter, String date, String outFormatter)
{
String newFormatedDate = "";
if (date != null)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(inFormatter);
Date oDate = null;
try
{
oDate = a_oFormatter.parse(date);
newFormatedDate = DateTimeUtils.formatedDateTime(outFormatter, oDate);
}
catch (ParseException ex)
{
//UtilsTrace.catching(DateTimeUtils.class, "convertDateTime", ex);
}
}
return newFormatedDate;
}
/**
* Returns the current EST time, used in histroy page.
*/
public static Date getCurrentEstTime()
{
TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
GregorianCalendar estDate = new GregorianCalendar (est);
return estDate.getTime();
}
/**
* Returns a string formatted
*/
public static String formatEstDate(Date date)
{
TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
dateFormatter.setTimeZone(est);
return dateFormatter.format(date);
}
/**
* Returns current EST Timestamp as string in format yyyy-MM-dd'T'hh:mm:ss:SSSS
*/
public static String getCurrentEstTimeStamp()
{
TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
SimpleDateFormat dateFormatter = new SimpleDateFormat(TIME_STAMP, Locale.US);
dateFormatter.setTimeZone(est);
return dateFormatter.format(getCurrentEstTime());
}
public static void main(String[] args){
// this will get China Standard time, seems that will base the server time zone.
System.out.println(getCurrentEstTime()); //Tue Nov 19 16:52:17 CST 2013
System.out.println(getCurrentEstTimeStamp());//2013-11-19T03:52:17:0264
System.out.println(formatEstDate(getCurrentEstTime()));//Tuesday, November 19, 2013 3:52:17 AM EST
// format a string date from one pattern to another.
System.out.println(convertDateTime(YYYYMMDD,"20131119",BACKLASH_MM_DD_YYYY));
}
}
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
/**
* This class contains utility methods to deal with dates.
*
*/
public class DateTimeUtils
{
public static final String YYYYMMDD = "yyyyMMdd";
public static final String BACKLASH_MM_DD_YYYY = "MM/dd/yyyy";
public static final String DASH_MMDDYYYY = "yyyy-MM-dd";
public static final String TIME_STAMP = "yyyy-MM-dd'T'hh:mm:ss:SSSS";
/**
* Returns current date and time depending on format string passed in
*/
public static String formatedCurrentDateTime(String formatterString)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(formatterString);
Date a_oCurrentTime = new Date();
return a_oFormatter.format(a_oCurrentTime);
}
/**
* Returns date and time depending on format string and date passed in
*/
public static String formatedDateTime(String formatterString, Date date)
{
String sformat = "";
if (date != null)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(formatterString);
sformat = a_oFormatter.format(date);
}
return sformat;
}
/**
* Returns a Date object from a simple date format + the date
*/
public static Date getDateFromDateFormat(String inFormatter, String date)
{
Date oDate = null;
if (date != null)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(inFormatter);
try
{
oDate = a_oFormatter.parse(date);
}
catch (ParseException ex)
{
// UtilsTrace.catching(DateTimeUtils.class, "getDateFromDateFormat", ex);
}
}
return oDate;
}
/**
* Converts one date format to another
*/
public static String convertDateTime(String inFormatter, String date, String outFormatter)
{
String newFormatedDate = "";
if (date != null)
{
SimpleDateFormat a_oFormatter = new SimpleDateFormat(inFormatter);
Date oDate = null;
try
{
oDate = a_oFormatter.parse(date);
newFormatedDate = DateTimeUtils.formatedDateTime(outFormatter, oDate);
}
catch (ParseException ex)
{
//UtilsTrace.catching(DateTimeUtils.class, "convertDateTime", ex);
}
}
return newFormatedDate;
}
/**
* Returns the current EST time, used in histroy page.
*/
public static Date getCurrentEstTime()
{
TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
GregorianCalendar estDate = new GregorianCalendar (est);
return estDate.getTime();
}
/**
* Returns a string formatted
*/
public static String formatEstDate(Date date)
{
TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
dateFormatter.setTimeZone(est);
return dateFormatter.format(date);
}
/**
* Returns current EST Timestamp as string in format yyyy-MM-dd'T'hh:mm:ss:SSSS
*/
public static String getCurrentEstTimeStamp()
{
TimeZone est = TimeZone.getTimeZone ( "America/New_York" );
SimpleDateFormat dateFormatter = new SimpleDateFormat(TIME_STAMP, Locale.US);
dateFormatter.setTimeZone(est);
return dateFormatter.format(getCurrentEstTime());
}
public static void main(String[] args){
// this will get China Standard time, seems that will base the server time zone.
System.out.println(getCurrentEstTime()); //Tue Nov 19 16:52:17 CST 2013
System.out.println(getCurrentEstTimeStamp());//2013-11-19T03:52:17:0264
System.out.println(formatEstDate(getCurrentEstTime()));//Tuesday, November 19, 2013 3:52:17 AM EST
// format a string date from one pattern to another.
System.out.println(convertDateTime(YYYYMMDD,"20131119",BACKLASH_MM_DD_YYYY));
}
}