问题
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