0

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

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Hitarth
  • 1,950
  • 3
  • 27
  • 52

4 Answers4

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);
Govindarao Kondala
  • 2,862
  • 17
  • 27
0

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) {       
}
Matthias Bruns
  • 899
  • 8
  • 13
0
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.

Community
  • 1
  • 1
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
-1
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();
Vaandu
  • 4,857
  • 12
  • 49
  • 75
Suresh
  • 1,494
  • 3
  • 13
  • 14