com.google.api.ads.dfp.lib.utils
Class DateTimesHelper<T,D>

java.lang.Object
  extended by com.google.api.ads.dfp.lib.utils.DateTimesHelper<T,D>
Type Parameters:
T - the DateTime type
D - the Date type

public class DateTimesHelper<T,D>
extends Object

A utility class for handling DateTime objects.

Author:
Adam Rogal

Constructor Summary
DateTimesHelper(Class<T> dateTimeClass, Class<D> dateClass)
          Constructor.
 
Method Summary
 Calendar toCalendar(T dateTime)
          Gets a calendar for a DateTime using the default locale, i.e.
 Calendar toCalendar(T dateTime, Locale locale)
          Gets a calendar for a DateTime in the supplied locale.
 T toDateTime(Calendar calendar)
          Converts a Calendar object to an API date time preserving the time zone.
 T toDateTime(org.joda.time.DateTime dateTime)
          Converts a DateTime object to an API date time preserving the time zone.
 T toDateTime(org.joda.time.Instant instant, String timeZoneId)
          Converts an Instant object to an API date time in the time zone supplied.
 T toDateTime(String dateTime, String timeZoneId)
          Converts a string in the form of yyyy-MM-dd'T'HH:mm:ss to an API date time in the time zone supplied.
 org.joda.time.DateTime toDateTime(T dateTime)
          Converts an API date time to a DateTime preserving the time zone.
 String toString(T dateTime)
          Returns string representation of this date time.
 String toStringWithZone(T dateTime, String newZoneID)
          Returns string representation of this date time with a different time zone, preserving the millisecond instant.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateTimesHelper

public DateTimesHelper(Class<T> dateTimeClass,
                       Class<D> dateClass)
Constructor.

Parameters:
dateTimeClass - the date time class
dateClass - the date class
Method Detail

toDateTime

public T toDateTime(Calendar calendar)
Converts a Calendar object to an API date time preserving the time zone.


toDateTime

public T toDateTime(org.joda.time.Instant instant,
                    String timeZoneId)
Converts an Instant object to an API date time in the time zone supplied.


toDateTime

public T toDateTime(org.joda.time.DateTime dateTime)
Converts a DateTime object to an API date time preserving the time zone.


toDateTime

public T toDateTime(String dateTime,
                    String timeZoneId)
Converts a string in the form of yyyy-MM-dd'T'HH:mm:ss to an API date time in the time zone supplied.


toDateTime

public org.joda.time.DateTime toDateTime(T dateTime)
Converts an API date time to a DateTime preserving the time zone.


toCalendar

public Calendar toCalendar(T dateTime)
Gets a calendar for a DateTime using the default locale, i.e. Locale.getDefault().


toCalendar

public Calendar toCalendar(T dateTime,
                           Locale locale)
Gets a calendar for a DateTime in the supplied locale.


toString

public String toString(T dateTime)
Returns string representation of this date time. The string representation does not include the time zone since using date times for filtering does not use the time zone. If you need to convert the date time into another time zone before filtering on it, please use toStringWithZone(Object, String) instead.

Parameters:
dateTime - the date time to stringify
Returns:
a string representation of the DateTime in yyyy-MM-dd'T'HH:mm:ss

toStringWithZone

public String toStringWithZone(T dateTime,
                               String newZoneID)
Returns string representation of this date time with a different time zone, preserving the millisecond instant.

This method is useful for finding the local time in another time zone, especially for filtering.

For example, if this date time holds 12:30 in Europe/London, the result from this method with Europe/Paris would be 13:30. You may also want to use this with your network's time zone, i.e.

 String timeZoneId = networkService.getCurrentNetwork().getTimeZone();
 String statementPart =
     "startDateTime > "
         + DateTimes.toString(apiDateTime, timeZoneId);
 //...
 statementBuilder.where(statementPart);
 
This method is in the same style of DateTime.withZone(org.joda.time.DateTimeZone).

Parameters:
dateTime - the date time to stringify into a new time zone
newZoneID - the time zone ID of the new zone
Returns:
a string representation of the DateTime in yyyy-MM-dd'T'HH:mm:ss


Copyright © 2012. All Rights Reserved.