How to add one hour to UTC in android - java

I'm trying get current time and then, convert to UTC+1 time zone which is in yyyy-MM-dd HH:mm:ss format
I tried the following code. It didn't work. Can anyone help?
public static String getDateTime() {
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC+1"));
return outputFmt.format(time);
}

I think you can try:
public static String getDateTime() {
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Read current time already in desired timezone
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
return outputFmt.format(time.getTime());
}

Following method will add one hour to UTC time.
public static String getDateTime()
{
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
String utctime=outputFmt.format(new Date());
String time=null;
Calendar calendar=null;
Date date;
try
{
calendar = Calendar.getInstance();
calendar.setTime(outputFmt.parse(utctime));
calendar.add(Calendar.HOUR, 1);
date=calendar.getTime();
time=outputFmt.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}

Related

Changing timezone of the date to UTC on API Level 21

I have a Date and I need to change timezone of this date to UTC. The code below does not work.
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(dateLocal.getTime());
return new Date(cal.getTimeInMillis());
On the Stackoverflow all examples return either a String or use API 26. How can I solve my problem on the Android API 21?
Try this:
public Date getDateInUtc() {
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
Date dateToReturn = null;
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
String utcTime = sdf.format(new Date());
try {
dateToReturn = dateFormat.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn;
}
Use cal.getTime(); which returns date
The same result you can get without SimpleDateFormat:
public Date dateInUtc(Date input) {
Calendar inputCalendar = Calendar.getInstance();
inputCalendar.setTime(input);
TimeZone timeZone = inputCalendar.getTimeZone();
long timeInUtc = input.getTime() - timeZone.getRawOffset();
Calendar outputCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
outputCalendar.setTimeInMillis(timeInUtc);
return outputCalendar.getTime();
}

I want this method to convert and get value in miliseconds [duplicate]

This question already has answers here:
How to convert a date to milliseconds
(5 answers)
Closed 3 years ago.
How can I do that? I tried a lot but it is not working properly.
public static String getDateTime() {
String dateStr = "";
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateStr = dateFormat.format(date);
return dateStr;
}
Please someone right another method which return miliseconds from above Date format. Of course! In my DB I'm storing this value: 2019-04-17 17:11:02 -> I want this value to convert in miliseconds using one method. so that I call new method and pass that variable value.
public static long milisecondsFromDate(String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = formatter.parse(dateStr);
return date.getTime();
} catch (Exception e) {
Log.e("Tag", "Wrong date Format");
}
return -1;
}
I would strongly suggest to use Java8 Date/Time API for your cause, something like:
public static void main(String[] args) {
System.out.println(getDateTime());
}
public static String getDateTime() {
String dateStr = "2019-04-17 17:11:02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);
return String.valueOf(localDateTime.toInstant(ZoneOffset.UTC).getEpochSecond());
}
try this:-
long timeInMilliseconds = 0;
String givenDateString = "2019-04-17 17:11:02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
String timeInMilliseconds = String.valueOf(timeInMilliseconds);

android utc time conversion issue

I need to convert a UTC time string into my local time, the code works fine in my PC when I run it on Eclipse, however, I get incorrect results on Android. I am stuck, can someone help.
Below is the code I am using, an example input would be "2017-04-24 1:00 AM", this will yield a result of "11:00 AM" which is what I am expecting, however, it always return "10:00 AM" on Android.
public String convertUTCTime(String utcTime) {
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm a");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date date = sdf.parse(utcTime);
sdf = new SimpleDateFormat("HH:mm a");
sdf.setTimeZone(tz);
String format = sdf.format(date);
return format;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Not applicable";
}
Look I prepared function:-
public static String getUTCDate(String timestamp, String DateFormat) {
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm aa", Locale.getDefault());
originalFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat targetFormat = new SimpleDateFormat(DateFormat, Locale.getDefault());
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
String formattedDate = null;
try {
date = originalFormat.parse(timestamp);
formattedDate = targetFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
Get the value by calling -
Utils.getUTCDate("2017-04-24 01:00 AM", "HH:mm a")

Android: how to get the timezone string in the format specified here

How can I obtain the timezone format and return the string like so
https://msdn.microsoft.com/en-us/library/gg154758.aspx - column Time zone name,
for example "SA Pacific Standard Time"
or worse comes to worst, how can I obtain it like so: (UTC-05:00) Bogota, Lima, Quito, or at least (UTC-05:00)
so that I can manually match it to the former String if I put them all in a map?
Have you tried?
TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
Result be if your device happens to be in Australia
TimeZone GMT+09:30 Timezon id :: Australia/Darwin
This should return UTC as a String and Date object. You can change the date format.
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"
public static Date getUTCdatetimeAsDate()
{
//note: doesn't check for null
return stringDateToDate(GetUTCdatetimeAsString());
}
public static String getUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
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(DATEFORMAT);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
This might be irrelevant (don't know if Android fully supports java-8), but you can do it using standard java api:
ZonedDateTime dateTime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Asia/Tokyo]");
System.out.println(dateTime); // 2007-12-03T10:15:30+09:00[Asia/Tokyo]
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("(O) z");
System.out.println(dateTime.format(formatter)); // (GMT+9) JST

Date to XMLGregorianCalendar with specific format

I am getting a Date object, which i need to convert to XMLGregorian Calendar specific format
I tried below ways
String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(formattedDate);
dataListType.setTimestamp(xmlCal);
I am getting an exception, for sure I am doing wrong here. But I want to format the Date object into specified format, which is done by sdf.format perfectly.
But how do I create the XMLGregorianCalendar object for the same (from formattedDate)?
You should fixed your date format:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = sdf.format(new Date());
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(date);
You can do it by the date object itself:
String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss
convertStringToXmlGregorian(formattedDate);
public XMLGregorianCalendar convertStringToXmlGregorian(String dateString)
{
try {
Date date = sdf.parse(dateString);
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
gc.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (ParseException e) {
// Optimize exception handling
System.out.print(e.getMessage());
return null;
}
}

Categories

Resources