Skip to content

Instantly share code, notes, and snippets.

@kharmabum
Created June 28, 2015 08:24
Show Gist options
  • Save kharmabum/73c69b6e4d5521cc3431 to your computer and use it in GitHub Desktop.
Save kharmabum/73c69b6e4d5521cc3431 to your computer and use it in GitHub Desktop.
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class UTCDate {
public static final String Date = "MMM dd, yyyy HH:mm:ss";
// public static final String DateFormat = "MM/dd/yyyy HH:mm:ss:SSS";
public static Date GetUTCdatetimeAsDate()
{
return StringDateToDate(GetUTCdatetimeAsString());
}
/**
*
* this function to get date time in defined format
* @return : String
* @return
*/
public static String GetUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(Date);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
return utcTime;
}
public static Date StringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(Date);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
public static String convertDateStringUTCToLocal(String sourceUtcDateTimeString)
{
SimpleDateFormat simpleDataFormat = new SimpleDateFormat();
simpleDataFormat.setTimeZone(getCurrentTimeZone());
String outputUTCDateTimeString = simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0)).toString();
return outputUTCDateTimeString;
}
public static TimeZone getCurrentTimeZone()
{
Calendar calendar = Calendar.getInstance();
TimeZone outputTimeZone = calendar.getTimeZone();
return outputTimeZone;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment