i have string looks "2015-06-07 16:01:33.0". want convert unix timestamp. first use calendar utility convert date object. how can convert date epoch time?
string origintime = "2015-06-07 16:01:33.0"; string year = origintime.split(" ")[0].split("-")[0]; string month = origintime.split(" ")[0].split("-")[1]; string day = origintime.split(" ")[0].split("-")[2]; string hour = origintime.split(" ")[1].split(":")[0]; string mins = origintime.split(" ")[1].split(":")[1]; string secs = origintime.split(" ")[1].split(":")[2].replace(".0",""); calendar cal = calendar.getinstance(); cal.set(calendar.day_of_month, integer.parseint(day)); cal.set(calendar.month, integer.parseint(month)); cal.set(calendar.year, integer.parseint(year)); cal.set(calendar.hour_of_day,integer.parseint(hour)); cal.set(calendar.minute,integer.parseint(mins)); cal.set(calendar.second,integer.parseint(secs)); string strdate = null; simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); if (cal != null) { strdate = sdf.format(cal.gettime()); } system.out.println(strdate);
i think can search answers @ stackoverflow, instead of posting new question, since make stackoverflow better, did quick search , here are: getting unix timestamp date()
or here code straight forward:
date currentdate = new date(); currentdate.gettime() / 1000; recently, people prefer jodatime:
datetime datetime = new datetime(); long unix = datetime.getmillis()/1000;
Comments
Post a Comment