Convert a Date and Time selected by user to timestamp - java

Can someone let me know how do you convert a User selected Date and Time to Timestamp value? Preferably UNIX timestamp.

Code for convert Date and Time to Unix Timestamp (for datetime format "yyyy-MM-dd HH:mm:ss")
try {
Date utilDate;
SimpleDateFormat sqlDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String sqlDate = "2014-01-01 10:10:10";
Calendar calTempDate = Calendar.getInstance();
calTempDate.setFirstDayOfWeek(Calendar.MONDAY);
utilDate = sqlDateFormat.parse(sqlDate);
calTempDate.setTime(utilDate);
long dt = (calTempDate.getTimeInMillis()) / 1000;
System.out.println("Timestamp is: " + dt);
} catch (ParseException e) {
e.printStackTrace();
}

Related

SimpleDateFormat not formatting offset date time correctly

I got a timestamp as follows, 2019-10-17T07:10:39.021+10:30 but when I parse through the SimpleDateFormat then print again, it appear as 2019-10-17T07:40:39.021+11:00
Looks like it added the 30min to time then change the time zone. Is there is a way to fix that.
Date date = null;
String value = "2019-10-17T07:10:39.021+10:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault());
System.out.println("input :" + value);
try {
date = sdf.parse(value);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("output :" + sdf.format(date));
Result
input :2019-10-17T07:10:39.021+10:30
output :2019-10-17T07:40:39.021+11:00
Should be same as input.
The date string you have 2019-10-17T07:10:39.021+10:30consists of offset, so from java-8 you can use OffsetDateTime
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime dateTime = OffsetDateTime.parse(date);
System.out.println(dateTime.toString()); //2019-10-17T07:10:39.021+10:30
Why are you using Locale.getDefault(), that parameter is not necessary. Can you try just calling it as below,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

Calendar.getTime() to Date object

I've an instance of a Calendar setted with UTC time zone, I need to be UTC becouse I've to sync with a server that is UTC.
I need to create a Date object from this Calendar, and I use Calendar.getTime().
But when I try to print out the Date object I see it with a different TimeZone (CEST instead of UTC)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(timeMillisecond);
Date d = cal.getTime();
Log.d("TAG", d.toString());
EDIT:
When I pass the date object to the server, I get it with CEST timezone instead of UTC time zone.
You can use sdf, set its timezone and parse it accordingly.
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
try {
Date dt = sdf.parse(sdf.format(Calendar.getInstance()));
} catch (ParseException e) {
e.printStackTrace();
}
This worked for me.
Try the below code, it will provide you the date in UTC.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy dd,MM hh:mm:ss", Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String utcTime = dateFormat.format(new Date(timeMillisecond));
Log.d("TAG", utcTime);
It will provide you the date in yyyy dd,MM hh:mm:ss format but you can provide other format according to your need.

Convert UTC time to local time java Android

Im retrieving some values from a json service and the retrieved datetime values are stored in UTC format.
i've tried a lot of sample codes to convert the datetime values to user local timezone but im still getting the same value after conversion.
This is what i have actually: (copied from other posts)
String sJsonDate = "2015-07-08T12:08:13.0625+00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date localDateTime = simpleDateFormat.parse(sJsonDate);
} catch (ParseException e) {
e.printStackTrace();
}
The result value (localDateTime) is the same as the original value.
I am in Paraguay (GMT-4) and the resulting values needs to be minus one hour diference, like this: ("2015-07-08 07:13:25") (The values are stored in Argentina)
Help please!
I've found the solution, we are using day light savings so i had to disccount one hour to the resulting datetime.
So, i share the code for someone else:
public Date getDateInTimeZone(Date currentDate, String timeZoneId) {
TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
Date localDateTime = new Date(currentDate.getTime() + timeZone.getOffset(currentDate.getTime()));
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(localDateTime.getTime());
if (timeZone.useDaylightTime()) {
// time zone uses Daylight Saving
cal.add(Calendar.MILLISECOND, timeZone.getDSTSavings() * -1);// in milliseconds
}
return cal.getTime();
}
Usage:
String sDate = "2015-07-08T12:08:13.0625+00:00";
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date theDate = simpleDateFormat.parse(sDate);
Date localDateTime = getDateInTimeZone(theDate, TimeZone.getDefault().getID());
} catch (ParseException e) {
e.printStackTrace();
}

How to convert java.util.Date to GMT format

I have a string "2014-07-02T17:12:36.488-01:00" which shows the Mountain time zone. I parsed this into java.util.date format. Now I need to convert this to GMT format. Can anyone help me??
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Object dd = null;
try {
dd=sdf.parseObject("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();`enter code here`
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:+ gmtDateFormat.format(dd));
There are a few problems in your code. For example, the format string doesn't match the actual format of the string you are parsing.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Object dd = null;
try {
dd = sdf.parse("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd));
To print the current date in whatever timezone you like, set the timezone you want to use on the SimpleDateFormat object. For example:
// Create a Date object set to the current date and time
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(now));
df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println("Current date and time in IST: " + df.format(now));

Java convert UTC timestamp to local DateTime

I know there are dozens of answered posts about converting UTC Time/Date To/From local time already but non helped me to figure out what my problem is.
My question is:
By having UTC timestamp, how can i get local DateTime?
This is what I have right now but this just convert the timestamp to DateTime format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getDefault());
sdf.format(new Date(timestamp * 1000));
Edited: I'm saving the UTC timestamp on the cloud so every device (Android/iOS) can query and convert to it's time zone.
Try this is working with me
public String getDateCurrentTimeZone(long timestamp) {
try{
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault();
calendar.setTimeInMillis(timestamp * 1000);
calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currenTimeZone = (Date) calendar.getTime();
return sdf.format(currenTimeZone);
}catch (Exception e) {
}
return "";
}
You can try this
String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss a z" ;
final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateTimeString = sdf.format(new Date());
System.out.println(dateTimeString); // current UTC time
long timeStamp=sdf.parse(dateTimeString).getTime(); //current UTC time in milisec
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(timeStamp));
cal.add(Calendar.HOUR_OF_DAY, 5);
cal.add(Calendar.MINUTE, 30);
System.out.println(sdf.format(cal.getTime())); // time relevant to UTC+5.30
U can use Joda time to convert local to UTC and vice-versa
e.g Local to UTC
DateTime dateTimeNew = new DateTime(date.getTime(), DateTimeZone.forID("Asia/Calcutta"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String datetimeString = dateTimeNew.toString("yyyy-MM-dd HH:mm:ss");
long milis = 0;
try {
milis = simpleDateFormat.parse(datetimeString).getTime();
} catch (ParseException e) {
e.printStackTrace();
}

Categories