import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.cpplus.camera.R.string;
import com.cpplus.camera.logger.Category;
import com.cpplus.camera.logger.Logger;
import com.cpplus.camera.logger.VCLog;
/**
* GMT (UTC) Time Converter.
*
* E.g.: GMTTime = "2009-10-09T00:37:22Z";
*
*/
public class DateTimeUtils {
/**
* "Tue Feb 26 2013 03:37 pm " // TimeZone is Kolkata
*/
public static String ALERT_SYNC_TIMESTAMP_FORMAT = "EEE, MMM dd, yyyy hh:mm:ss aa";
public static String ALERT_NOTIFICATION_TIMESTAMP_FORMAT = "dd-MM-yyyy hh:mm:ss aa";
public static String EVENT_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static SimpleDateFormat dateFormatter = new SimpleDateFormat( "MM/dd/yyyy", Locale.US );
private static SimpleDateFormat timeFormatter12 = new SimpleDateFormat( "h:mm:ss aa", Locale.US );
private static SimpleDateFormat timeFormatter24 = new SimpleDateFormat( "HH:mm:ss", Locale.US );
private static SimpleDateFormat GMTTimeFormatter = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" );
private static SimpleDateFormat ALERTS_DATE_FORMATTER = new SimpleDateFormat( "yyyy-MM-dd" );
{
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
}
/**
* This function gets called ,to check whether the given string is in GMT format and returns a
* boolean value
*
* @param strGMTTime
* : Instance of {@link string}
* @return true if is in GMT format otherwise returns false
*/
public static boolean isGMTTimeFormat( String strGMTTime ) {
boolean isValid = false;
try {
GMTTimeFormatter.parse( strGMTTime );
isValid = true;
} catch ( ParseException e ) {
}
if ( strGMTTime.length( ) != 20 )
isValid = false;
return isValid;
}
/**
* This function gets the GMT time in string format and converts it following format
* "Tue Feb 26 2013 16:05:00 " // TimeZone is Kolkata, zzz=[GMT+05:30 in Android | CST in Java]
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getLocalDateString24Format( String strGMTTime ) {
String strLocalDate = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
dateFormatter.setTimeZone( TimeZone.getDefault( ) );
strLocalDate = dateFormatter.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalDate;
}
/**
* This function gets the GMT time in string format and converts it following format
* "Tue Feb 26 2013 16:05:00 " // TimeZone is Kolkata, zzz=[GMT+05:30 in Android | CST in Java]
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getLocalDateString24AlertsFormat( String strGMTTime ) {
String strLocalDate = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
ALERTS_DATE_FORMATTER.setTimeZone( TimeZone.getDefault( ) );
strLocalDate = ALERTS_DATE_FORMATTER.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalDate;
}
public static String getLocalTimeString24Format( String strGMTTime ) {
String strLocalTime = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
timeFormatter24.setTimeZone( TimeZone.getDefault( ) );
strLocalTime = timeFormatter24.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalTime;
}
/**
* This function gets the GMT time in string format and converts it following format
* "Tue Feb 26 2013 03:37 pm "
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getLocalDateString12Format( String strGMTTime ) {
String strLocalDate = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
dateFormatter.setTimeZone( TimeZone.getDefault( ) );
strLocalDate = dateFormatter.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalDate;
}
/**
* This function gets the local GMT time in string format and converts it UTC format
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getUTCDateTimeString( String strGMTTime ) {
String strUtcDate = null;
try {
SimpleDateFormat UTCTimeFormatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date date = UTCTimeFormatter.parse( strGMTTime );
UTCTimeFormatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
strUtcDate = UTCTimeFormatter.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strUtcDate;
}
public static String getLocalTimeString12Format( String strGMTTime ) {
String strLocalTime = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
timeFormatter12.setTimeZone( TimeZone.getDefault( ) );
strLocalTime = timeFormatter12.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalTime;
}
/**
* Method to get the formatted time
*
* @param format
* the format
* @return the formatted time.
*/
public static String getFormattedTime( String format ) {
String time = null;
long currentTimeMillis = System.currentTimeMillis( );
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( format );
time = simpleDateFormat.format( new Date( currentTimeMillis ) );
return time;
}
public static boolean isWithinRange( String fromDate, String toDate, String date, String format ) {
try {
SimpleDateFormat dateFormatter = new SimpleDateFormat( format, Locale.US );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date1 = dateFormatter.parse( fromDate );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date2 = dateFormatter.parse( toDate );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date3 = dateFormatter.parse( date );
if ( date3.getTime( ) <= date2.getTime( ) && date3.getTime( ) >= date1.getTime( ) ) {
return true;
}
} catch ( Exception e ) {
}
return false;
}
/**
* Method to compare to date strings.
*
* @param strDate1
* string date1
* @param strDate2
* string date1
* @return true if date1 is after date2, false otherwise.
*/
public static boolean compare( String strDate1, String strDate2 ) {
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date1 = GMTTimeFormatter.parse( strDate1 );
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date2 = GMTTimeFormatter.parse( strDate2 );
if ( date1.after( date2 ) ) {
return true;
}
} catch ( Exception e ) {
}
return false;
}
public static boolean compare( String strDate1, String strDate2, String format ) {
try {
SimpleDateFormat dateFormatter = new SimpleDateFormat( format, Locale.US );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date1 = dateFormatter.parse( strDate1 );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date2 = dateFormatter.parse( strDate2 );
if ( date1.after( date2 ) ) {
return true;
}
} catch ( Exception e ) {
}
return false;
}
/**
* Method to get the no of dates between two specified dates.
*
* @param strDate1
* the startDate
* @param strDate2
* the endDate
* @return the no of days between input dates
*/
public static int getNoOfDaysBetween( String strDate1, String strDate2 ) {
long diff = 0;
try {
SimpleDateFormat format = new SimpleDateFormat( Logger.FILE_NAME_FORMAT, Locale.US );
Date date1 = format.parse( strDate1 );
Date date2 = format.parse( strDate2 );
diff = ( (long) ( date1.getTime( ) - date2.getTime( ) ) ) / ( 1000 * 60 * 60 * 24 );
} catch ( Exception ex ) {
VCLog.error( Category.CAT_GENERAL, "DateTimeUtils: getNoOfDaysBetween: Exception->" + ex.getMessage( ) );
}
return (int) diff;
}
/**
* Method to get the TimeZone. Returns the TimeZone in GMT+-xx:xx format
*/
public static String getTimeZone() {
String timeZone = "GMT";
Calendar calendar = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.getDefault( ) );
Date currentLocalTime = calendar.getTime( );
SimpleDateFormat date = new SimpleDateFormat( "Z" );
String localTime = date.format( currentLocalTime );
// +0530
if ( localTime != null ) {
if ( isDayLightSavingEnabled( ) ) {
/**
* If DST is enabled, add one hour to it
*/
timeZone += localTime.substring( 0, 1 );
int hour = Integer.parseInt( localTime.substring( 1, localTime.length( ) - 2 ) );
++hour;
if ( hour < 10 ) {
/**
* Prefix 0 to hour if < 10
*/
timeZone += "0" + hour;
} else {
timeZone += hour;
}
} else {
timeZone += localTime.substring( 0, localTime.length( ) - 2 );
}
timeZone += ":" + localTime.substring( localTime.length( ) - 2 );
}
VCLog.debug( Category.CAT_GUI, "DateTimeUtils: onCreate: timezone->" + timeZone );
return timeZone;
}
/**
* Method to check if the day light saving is enabled.
*
* @return true if day light saving is enabled, false otherwise.
*/
public static boolean isDayLightSavingEnabled() {
String timezoneId = TimeZone.getDefault( ).getID( );
return TimeZone.getTimeZone( timezoneId ).inDaylightTime( new Date( ) );
}
public static String getFormattedDateTimeForAlertVideo( String format, String strGMTDateTime ) {
String dateTime = "";
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTDateTime );
SimpleDateFormat dateFormat = new SimpleDateFormat( format );
dateFormat.setTimeZone( TimeZone.getDefault( ) );
dateTime = dateFormat.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return dateTime;
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.cpplus.camera.R.string;
import com.cpplus.camera.logger.Category;
import com.cpplus.camera.logger.Logger;
import com.cpplus.camera.logger.VCLog;
/**
* GMT (UTC) Time Converter.
*
* E.g.: GMTTime = "2009-10-09T00:37:22Z";
*
*/
public class DateTimeUtils {
/**
* "Tue Feb 26 2013 03:37 pm " // TimeZone is Kolkata
*/
public static String ALERT_SYNC_TIMESTAMP_FORMAT = "EEE, MMM dd, yyyy hh:mm:ss aa";
public static String ALERT_NOTIFICATION_TIMESTAMP_FORMAT = "dd-MM-yyyy hh:mm:ss aa";
public static String EVENT_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static SimpleDateFormat dateFormatter = new SimpleDateFormat( "MM/dd/yyyy", Locale.US );
private static SimpleDateFormat timeFormatter12 = new SimpleDateFormat( "h:mm:ss aa", Locale.US );
private static SimpleDateFormat timeFormatter24 = new SimpleDateFormat( "HH:mm:ss", Locale.US );
private static SimpleDateFormat GMTTimeFormatter = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" );
private static SimpleDateFormat ALERTS_DATE_FORMATTER = new SimpleDateFormat( "yyyy-MM-dd" );
{
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
}
/**
* This function gets called ,to check whether the given string is in GMT format and returns a
* boolean value
*
* @param strGMTTime
* : Instance of {@link string}
* @return true if is in GMT format otherwise returns false
*/
public static boolean isGMTTimeFormat( String strGMTTime ) {
boolean isValid = false;
try {
GMTTimeFormatter.parse( strGMTTime );
isValid = true;
} catch ( ParseException e ) {
}
if ( strGMTTime.length( ) != 20 )
isValid = false;
return isValid;
}
/**
* This function gets the GMT time in string format and converts it following format
* "Tue Feb 26 2013 16:05:00 " // TimeZone is Kolkata, zzz=[GMT+05:30 in Android | CST in Java]
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getLocalDateString24Format( String strGMTTime ) {
String strLocalDate = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
dateFormatter.setTimeZone( TimeZone.getDefault( ) );
strLocalDate = dateFormatter.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalDate;
}
/**
* This function gets the GMT time in string format and converts it following format
* "Tue Feb 26 2013 16:05:00 " // TimeZone is Kolkata, zzz=[GMT+05:30 in Android | CST in Java]
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getLocalDateString24AlertsFormat( String strGMTTime ) {
String strLocalDate = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
ALERTS_DATE_FORMATTER.setTimeZone( TimeZone.getDefault( ) );
strLocalDate = ALERTS_DATE_FORMATTER.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalDate;
}
public static String getLocalTimeString24Format( String strGMTTime ) {
String strLocalTime = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
timeFormatter24.setTimeZone( TimeZone.getDefault( ) );
strLocalTime = timeFormatter24.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalTime;
}
/**
* This function gets the GMT time in string format and converts it following format
* "Tue Feb 26 2013 03:37 pm "
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getLocalDateString12Format( String strGMTTime ) {
String strLocalDate = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
dateFormatter.setTimeZone( TimeZone.getDefault( ) );
strLocalDate = dateFormatter.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalDate;
}
/**
* This function gets the local GMT time in string format and converts it UTC format
*
* @param strGMTTime
* : Instance of {@link string}
*/
public static String getUTCDateTimeString( String strGMTTime ) {
String strUtcDate = null;
try {
SimpleDateFormat UTCTimeFormatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Date date = UTCTimeFormatter.parse( strGMTTime );
UTCTimeFormatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
strUtcDate = UTCTimeFormatter.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strUtcDate;
}
public static String getLocalTimeString12Format( String strGMTTime ) {
String strLocalTime = null;
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTTime );
timeFormatter12.setTimeZone( TimeZone.getDefault( ) );
strLocalTime = timeFormatter12.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return strLocalTime;
}
/**
* Method to get the formatted time
*
* @param format
* the format
* @return the formatted time.
*/
public static String getFormattedTime( String format ) {
String time = null;
long currentTimeMillis = System.currentTimeMillis( );
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( format );
time = simpleDateFormat.format( new Date( currentTimeMillis ) );
return time;
}
public static boolean isWithinRange( String fromDate, String toDate, String date, String format ) {
try {
SimpleDateFormat dateFormatter = new SimpleDateFormat( format, Locale.US );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date1 = dateFormatter.parse( fromDate );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date2 = dateFormatter.parse( toDate );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date3 = dateFormatter.parse( date );
if ( date3.getTime( ) <= date2.getTime( ) && date3.getTime( ) >= date1.getTime( ) ) {
return true;
}
} catch ( Exception e ) {
}
return false;
}
/**
* Method to compare to date strings.
*
* @param strDate1
* string date1
* @param strDate2
* string date1
* @return true if date1 is after date2, false otherwise.
*/
public static boolean compare( String strDate1, String strDate2 ) {
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date1 = GMTTimeFormatter.parse( strDate1 );
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date2 = GMTTimeFormatter.parse( strDate2 );
if ( date1.after( date2 ) ) {
return true;
}
} catch ( Exception e ) {
}
return false;
}
public static boolean compare( String strDate1, String strDate2, String format ) {
try {
SimpleDateFormat dateFormatter = new SimpleDateFormat( format, Locale.US );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date1 = dateFormatter.parse( strDate1 );
dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date2 = dateFormatter.parse( strDate2 );
if ( date1.after( date2 ) ) {
return true;
}
} catch ( Exception e ) {
}
return false;
}
/**
* Method to get the no of dates between two specified dates.
*
* @param strDate1
* the startDate
* @param strDate2
* the endDate
* @return the no of days between input dates
*/
public static int getNoOfDaysBetween( String strDate1, String strDate2 ) {
long diff = 0;
try {
SimpleDateFormat format = new SimpleDateFormat( Logger.FILE_NAME_FORMAT, Locale.US );
Date date1 = format.parse( strDate1 );
Date date2 = format.parse( strDate2 );
diff = ( (long) ( date1.getTime( ) - date2.getTime( ) ) ) / ( 1000 * 60 * 60 * 24 );
} catch ( Exception ex ) {
VCLog.error( Category.CAT_GENERAL, "DateTimeUtils: getNoOfDaysBetween: Exception->" + ex.getMessage( ) );
}
return (int) diff;
}
/**
* Method to get the TimeZone. Returns the TimeZone in GMT+-xx:xx format
*/
public static String getTimeZone() {
String timeZone = "GMT";
Calendar calendar = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.getDefault( ) );
Date currentLocalTime = calendar.getTime( );
SimpleDateFormat date = new SimpleDateFormat( "Z" );
String localTime = date.format( currentLocalTime );
// +0530
if ( localTime != null ) {
if ( isDayLightSavingEnabled( ) ) {
/**
* If DST is enabled, add one hour to it
*/
timeZone += localTime.substring( 0, 1 );
int hour = Integer.parseInt( localTime.substring( 1, localTime.length( ) - 2 ) );
++hour;
if ( hour < 10 ) {
/**
* Prefix 0 to hour if < 10
*/
timeZone += "0" + hour;
} else {
timeZone += hour;
}
} else {
timeZone += localTime.substring( 0, localTime.length( ) - 2 );
}
timeZone += ":" + localTime.substring( localTime.length( ) - 2 );
}
VCLog.debug( Category.CAT_GUI, "DateTimeUtils: onCreate: timezone->" + timeZone );
return timeZone;
}
/**
* Method to check if the day light saving is enabled.
*
* @return true if day light saving is enabled, false otherwise.
*/
public static boolean isDayLightSavingEnabled() {
String timezoneId = TimeZone.getDefault( ).getID( );
return TimeZone.getTimeZone( timezoneId ).inDaylightTime( new Date( ) );
}
public static String getFormattedDateTimeForAlertVideo( String format, String strGMTDateTime ) {
String dateTime = "";
try {
GMTTimeFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date date = GMTTimeFormatter.parse( strGMTDateTime );
SimpleDateFormat dateFormat = new SimpleDateFormat( format );
dateFormat.setTimeZone( TimeZone.getDefault( ) );
dateTime = dateFormat.format( date );
} catch ( ParseException e ) {
e.printStackTrace( );
}
return dateTime;
}
}