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

2019-09-18 11:24发布

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().

4条回答
手持菜刀,她持情操
2楼-- · 2019-09-18 12:05

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楼-- · 2019-09-18 12:08
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.

查看更多
Lonely孤独者°
4楼-- · 2019-09-18 12:21

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);
查看更多
迷人小祖宗
5楼-- · 2019-09-18 12:21
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();
查看更多
登录 后发表回答