-2

I have a String object that contains expired date and time like strDateTimeBoj="Oct 11 2014 5:30PM";

I need it to be extracted to separate date and time , because it needs to be checked with current date and time , whether this date is expired or not ...

I know the following code , but didn't achieved the goal

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

How to achieve this .....Any help would be great...

karthik
  • 29
  • 1
  • 7
  • 1
    Can you explain what you try to do. For me it is not clear what's he problem. If you whant to know if the date of the string is before or after now use the Calendar object. – Jens Sep 24 '14 at 07:39
  • It seems like you need to become more proficient at reading Javadocs. The docs for [`DateTimeFormat`](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html) explain what the different symbols mean. Surely you must have thought `"dd/MM/yyyy HH:mm:ss"` didn't look right? – Duncan Jones Sep 24 '14 at 07:40

4 Answers4

1

If you need two separate objects, 1 for date and another for time, then may be you can give this a try:

String strDateTimeBoj="Oct 11 2014  5:30PM";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
Date dateObj = dateFormat.parse(strDateTimeBoj);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date timeObj = dateFormat.parse(strDateTimeBoj);

But according to me, one object should do what you need to achieve.

String strDateTimeBoj="Oct 11 2014  5:30PM";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"); 
Date dateObj = dateFormat.parse(strDateTimeBoj);

And compare date with current date.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • how do I compare it with current date along with time , to know whether it is expired.. – karthik Sep 24 '14 at 08:03
  • @karthik refer [this](http://stackoverflow.com/questions/17132106/how-do-i-compare-current-date-with-user-input-date-from-date-picker) – MysticMagicϡ Sep 24 '14 at 08:07
0

Use following formatter:

String string="Oct 11 2014 5:30PM";
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy hh:mma");
Date dt = formatter.parse(string);
System.out.println(formatter.format(dt));

Output :

Oct 11 2014 05:30PM
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
  • DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"); DateTime dt = formatter.parseDateTime("Oct 11 2014 5:30PM"); Log.v("DateTimeFormatter", String.valueOf(dt)); – karthik Sep 24 '14 at 07:55
  • shows IllegalArgumentException: Invalid format: "Oct 11 2014 5:30PM" – karthik Sep 24 '14 at 07:55
0

You can try this way

 String strDateTimeBoj="Oct 11 2014 5:30PM";
 //first you need to use proper date formatter
 DateFormat df=new SimpleDateFormat("MMM dd yyyy hh:mmaa");
 Date date=df.parse(strDateTimeBoj);// converting String to date
 System.out.println(df.format(date));

Out put:

 Oct 11 2014 05:30PM

Which date format should I use?

You can find it out by your own by read this.

Edit

you can use before() after() method to compare dates.

Date today=new Date();

System.out.println(today.after(date));
System.out.println(today.before(date));

Out put:

false
true
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

You could get time in any format by using SimpleDateFormat class. Please refer to this Calendar cal = Calendar.getInstance(); String currentDate = cal.get(Calendar.DAY_OF_MONTH)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR); Date date = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.ENGLISH).parse(currentDate);

NIPHIN
  • 1,071
  • 1
  • 8
  • 16