-3

I am building groovy test and have the String in Properties:

date = new Date("09/15/2016")

Need to extract the String part with date like:

y=09/15/2016

I tried with substring method and it works but what if I want to use other methods like Regular expression or DateFormat?

msagala25
  • 1,806
  • 2
  • 17
  • 24

2 Answers2

0

This will work:

        Date date = new Date("09/15/2016");
        String strDate = new SimpleDateFormat("MM/dd/yy").format(date);
        System.out.println(strDate);
msagala25
  • 1,806
  • 2
  • 17
  • 24
0

As you've tagged this groovy, you can also do:

Date date = Date.parse("MM/dd/yyyy", "09/15/2016")

Update:

Ok...Weird... I didn't understand you actually had new Date(blah) as a String

This should work then, guess you do need a regexp ;-)

String weirdDateString = 'new Date("09/15/2016")'

String extractedDate = weirdDateString.find(/[0-9\/]+/)

Date date = Date.parse('MM/dd/yyyy', extractedDate)
tim_yates
  • 167,322
  • 27
  • 342
  • 338