I am able to get all user feed to facebook graph api in my android app. Following is date string from updated_time key :
2014-12-14T18:23:17+0000
First I wasn't able to find format for this string. From google I only come to know, this is might be RFC 2822 date format.
I want to convert this date string to unix time stamp to fetch new user feeds. How can I convert this string to unix time stamp?
If I try to parse this string using following formate I'm getting null date:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
It is advised to strip the +0000 from the date as this is the timezone information. Then handling the timezone separately i.e. as an integer you can either add or subtract it from the time in millis by multiplying the timezone stripped and converting it to milliseconds.
public static long getDateInMillis(String srcDate) {
try {
SimpleDateFormat desiredFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
long dateInMillis = 0;
try {
Date date = desiredFormat.parse(srcDate);
dateInMillis = date.getTime();
return dateInMillis;
} catch (ParseException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
Your format pattern is wrong.
String timeStamp = "2014-12-14T18:23:17+0000";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
System.out.println("Unix timestamp: " + dateFormat.parse(timeStamp).getTime());
Related
I am trying to convert a date in String format into UNIX timestamp, I am able to convert it but when I check the timestamp it displays incorrect date.
I am using the following code to convert a Date in String to Unix timestamp:
String selected_date = "16/11/2015 1:34 am";
datetime.setText(selected_date);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
Date date = null;
try {
date = dateFormat.parse(selected_date);
} catch (ParseException e) {
e.printStackTrace();
}
long unixTime = (long)date.getTime()/1000;
The output UNIX timestamp is: 1460309640
But when I convert that timestamp using a web tool it returns: 4/11/2016, 1:34:00 AM
The format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
is not compatible with the string
String selected_date = "16/11/2015 1:34 am";
16 can't be a month!
2015 is not a year in two digits format
The right format seems to be (not sure if kk or KK depending on hours if 0 based or not)
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy kk:mm a");
I receive date as string from web service in following format 2014-02-27T11:17:00.000Z Could someone tell me how to parse it as Date time object in Java.
I tried parsing it Date.parse() but it didn't work properly.
Then I tried date formatter but it crashes the app. Could someone enlighten me please.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date d = sdf.parse("2014-02-27T11:17:00.000Z");
You can use dateformat class for that.
DateFormat sdt = new SimpleDateFormat(put your format here);
Date stime= sdt.parse(starttime);
Date etime = sdt.parse(endtime);
Starttime and end time are the strings which you want to parse
Declare a SimpleDateTimeFormat to match your datetime from C# and then use .parse() method on it to get the (Java) Date.
Example:
private static final SimpleDateFormat FORMAT_FULL_DATE = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss'.000Z'"); // replace kk with hh for am/pm format
public static Date getDateTimeFromString(final String string) {
try {
return FORMAT_FULL_DATE.parse(string);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
My Date format is like as "MM-dd-yyyy hh:mm" its not current date ,I have to send this date
to server but before send it need to change this date to GMT format but when I change by following code:
private String[] DateConvertor(String datevalue)
{
String date_value[] = null;
String strGMTFormat = null;
SimpleDateFormat objFormat,objFormat1;
Calendar objCalendar;
Date objdate1,objdate2;
if(!datevalue.equals(""))
{
try
{
//Specify your format
objFormat1 = new SimpleDateFormat("MM-dd-yyyy,HH:mm");
objFormat1.setTimeZone(Calendar.getInstance().getTimeZone());
objFormat = new SimpleDateFormat("MM-dd-yyyy,HH:mm");
objFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
//Convert into GMT format
//objFormat.setTimeZone(TimeZone.getDefault());//);
objdate1=objFormat1.parse(datevalue);
//
//objdate2=objFormat.parse(datevalue);
//objFormat.setCalendar(objCalendar);
strGMTFormat = objFormat.format(objdate1.getTime());
//strGMTFormat = objFormat.format(objdate1.getTime());
//strGMTFormat=objdate1.toString();
if(strGMTFormat!=null && !strGMTFormat.equals(""))
date_value = strGMTFormat.split(",");
}
catch (Exception e)
{
e.printStackTrace();
e.toString();
}
finally
{
objFormat = null;
objCalendar = null;
}
}
return date_value;
}
its not change in required format ,I have tried by above code first try to get current timeZone and after that try change string date into that timezone after that convert GMT.
anyone guide me.
thanks in advance.
Try the below code. The first sysout prints the date object which picks up default OS timezone i.e. IST in my case. The second sysout prints the date in the required format after converting the date to GMT timezone.
If you know the timezone of your date string then set that in the formatter. I assumed you need the same date format in the GMT timezone.
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy,HH:mm");
Date date = format.parse("01-23-2012,09:40");
System.out.println(date);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(date));
you need to use TimeZone's getRawOffset() method:
Date localDate = Calendar.getInstance().getTime();
TimeZone tz = TimeZone.getDefault();
Date gmtDate = new Date(date.getTime() - tz.getRawOffset());
it
returns the amount of time in milliseconds to add to UTC to get standard time in this time zone. Because this value is not affected by daylight saving time, it is called raw offset.
If you want to consider DST as well (you might want this ;-) )
if (tz.inDaylightTime(ret)) {
Date dstDate = new Date(gmtDate.getTime() - tz.getDSTSavings());
if (tz.inDaylightTime(dstDate) {
gmtDate = dstDate;
}
}
The last check is needed if you are right on the edge of a summer time change and would, for instance, go back into standard time by the conversion.
Hope that helps,
-Hannes
I have written following program to convert date string to sql date object to store in db2 but the ouput shown is 2013-01-02 instead of 2013-02-02. can anyone explain why??
String string = "02/02/2013";
Date date = null;
try {
date = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(string);
java.sql.Date newDate=new java.sql.Date(date.getTime());
System.out.println(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
For month you should use MM instead of mm. mm is used for minutes in hour: -
date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
your format should be
"dd/MM/yyyy"
note that mm is for minutes whereas MM is for Month
Check the Doc
I have a field with temporal type as Timestamp to save both date and time to the database.
#Temporal(TemporalType.TIMESTAMP)
#Column(name="date", nullable=false)
private Date date;
But when displaying the value to the user, I just want to show date part and truncate time part. I tried this but I get ParseException probably because of the Nanosecond part of the Hibernate Timestamp.
SimplDateFormat sd = new SimpleDateFormat("MM/dd/yyyy");
return sd.parse(date.toString());
So how do I retrieve just date from Hibernate Timestamp? Thanks
Just format the date object. The toString method isn't guaranteed to give you a string in a format that can then be parsed.
String dateStr = sd.format(date);
That will give you a date string in MM/dd/yyyy format that you can then convert back into a Date.
Date fancyNancy = sd.parse(dateStr);
* EDIT *
Run this code and verify it prints out the day, month and year with no time.
try {
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Original Date: " + d);
System.out.println("Formatted Date: " + df.parse(df.format(d)));
} catch (Exception e) {
e.printStackTrace();
}