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();
}
Related
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;
}
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")
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
I want to set date and time in my android app. The date should be today's date but the time should be set to 6:00 AM by default in the text field. I have read many links but most of them shows today's time and date (example: 2016-03-28 11:53:55).
String timetxt1 = "06:00:00";
Date datetxt1 = null;;
try {
datetxt1 = simpleDateFormat.parse(timetxt1);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar selectedDate1 = Calendar.getInstance();
selectedDate1.setTime(datetxt1);
edittxt.setText(dateFormatter.format(selectedDate1.getTime()));
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 6);// for 6 hour
calendar.set(Calendar.MINUTE, 0);// for 0 min
calendar.set(Calendar.SECOND, 0);// for 0 sec
System.out.println(calendar.getTime());// print 'Mon Mar 28 06:00:00 ALMT 2016'
This was useful to me.
fun getFormattedDateTime(dateString: String):String{
var formattedDate=""
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())
val dateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val calendar = Calendar.getInstance()
calendar.time = dateFormat.parse(dateString)!!
calendar[Calendar.HOUR_OF_DAY]=6
calendar[Calendar.MINUTE]=0
calendar[Calendar.SECOND]=0
formattedDate=sdf.format(calendar.time)
return formattedDate
}
To get current date use below method
public static String getCurrentDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
This will give you the current date alone. And if you want the time to be 6:00AM use the below method
public static String getCurrentDateAndTime6() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE,0);
return dateFormat.format(calendar);
}
I've done like this,
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
date.setHours(6);
date.setMinutes(0);
date.setSeconds(0);
Log.d("DateTime", dateFormat.format(date));
OUTPUT : 2016/03/28 06:00:00
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;
}
}