android - Convert string back to java Date -
this question has answer here:
i have 1 date string in form of:
"sun sep 18 00:05:35 gmt+07:00 2016"
i getting format media store date taken. how convert date object in java/android?
the date in string can parsed existing date object using simpledateformat
class. creating simpledateformat
object , specifying date pattern in constructor able parse date stored in string.
for particular case code this:
simpledateformat format = new simpledateformat("eee mmm d hh:mm:ss z yyyy"); date date = null; try { date = format.parse(dateasstring); } catch (parseexception e) { e.printstacktrace(); }
as can seen pattern:
eee - day of week in 3 letters (e.g. sun, mon, tue, wed, thu, fri, sat)
mmm - month of year (between 0 , 11)
dd - day of month
hh - hour (between 00 , 23)
mm - minutes (between 00 , 59)
ss - seconds (between 00 , 59)
z - timezone (in case, general time zone - gmt)
yyyy - year
in addition, if further want pass date
object calendar
object need set time zone there too.
calendar calendar = calendar.getinstance(timezone.gettimezone("gmt+07:00")); calendar.settime(date);
however, if have timezone changing can extract timezone part string:
string timezone = dateasstring.substring(19, 28); calendar calendar = calendar.getinstance(timezone.gettimezone(timezone)); calendar.settime(date);
just careful starting , ending indexes of substring()
function
Comments
Post a Comment