yyyy-mm-dd and hh:mm (PM/AM) convert into long

和自甴很熟 提交于 2019-12-25 08:27:38

问题


I have two strings:

String date = "2011-11-11"
String time="11:00 PM" 

i want to merge this date and time and convert them into a long, similar to System.currentTimeMillis().


回答1:


try this it is working fine

 String inputDate=date+" "+time ;;
       long parsedDate = HttpDateParser.parse(inputDate);//inputDate should 2011-12-11 11:10:00 PM" formate
       System.out.println("========================="+parsedDate);
       Date date=new Date(parsedDate);
       SimpleDateFormat date1=new SimpleDateFormat("yyyy-mm-dd hh:mm aa");
       String opdate=date1.format(date);
       System.out.println("========================="+opdate);



回答2:


Use SimpleDateFormat and parse the String into a Date. When you have Date you can get .getTime() what's a long

http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

NOT TESTED!!

String date = "2011-11-11";
String time = "11:00 PM";
String toParse = date + " " + time;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm aa");
try {
    Date parse = sdf.parse(toParse);
    parse.getTime();
} catch (ParseException ex) {       
}



回答3:


String dateTime = "2011-11-11 " + time;
DateFormat formatter ; 
Date date ; 
formatter = new SimpleDateFormat("dd-MMM-yy HH:MM");
date = (Date)formatter.parse(dateTime ); 
long time = date.getTime();

I found this SO post in the same lines.




回答4:


String date =date+time ;
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm");
Date myDate = fmt.parse(date); 

System.out.println(myDate);  //Mon Jun 04 07:05:00 EDT 2007
long timestamp = myDate.getTime();


来源:https://stackoverflow.com/questions/8090196/yyyy-mm-dd-and-hhmm-pm-am-convert-into-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!