Asked  7 Months ago    Answers:  5   Viewed   83 times

I am trying to parse some dates that are coming out of a document. It would appear users have entered these dates in a similar but not exact format.

here are the formats:


What is the best way to go about trying to parse all of these? These seem to be the most common, but I guess what is hanging me up is that if i have a pattern of "M/yyyy" wont that always catch before "MM/yyyy" Do I have to set up my try/catch blocks nested in a least restrictive to most restrictive way? it seems like it sure is going to take a lot of code duplication to get this right.



You'll need to use a different SimpleDateFormat object for each different pattern. That said, you don't need that many different ones, thanks to this:

Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.

So, you'll need these formats:

  • "M/y" (that covers 9/09, 9/2009, and 09/2009)
  • "M/d/y" (that covers 9/1/2009)
  • "M-d-y" (that covers 9-1-2009)

So, my advice would be to write a method that works something like this (untested):

// ...
List<String> formatStrings = Arrays.asList("M/y", "M/d/y", "M-d-y");
// ...

Date tryParse(String dateString)
    for (String formatString : formatStrings)
            return new SimpleDateFormat(formatString).parse(dateString);
        catch (ParseException e) {}

    return null;
Tuesday, June 1, 2021
answered 7 Months ago

Try the following:

new Date(Date.parse(myArr[0][0]));


Use the Date.parse method to parse the string into the number of milliseconds since January 1, 1970, 00:00:00 UTC. Take that number of milliseconds and call the Date method once again to turn that time into a date object.


Well this may be a little ugly for this case, but it seems Firefox is having an issue with the -s and the 00.0.

var myArr = [["2012-10-10 03:47:00.0", 23.400000000000002], ["2012-10-10 03:52:00.0", 23.3]];

var date = convertDateTime(myArr[0][0]);

function convertDateTime(dateTime){
    dateTime = myArr[0][0].split(" ");

    var date = dateTime[0].split("-");
    var yyyy = date[0];
    var mm = date[1]-1;
    var dd = date[2];

    var time = dateTime[1].split(":");
    var h = time[0];
    var m = time[1];
    var s = parseInt(time[2]); //get rid of that 00.0;

    return new Date(yyyy,mm,dd,h,m,s);


Tuesday, August 3, 2021
answered 4 Months ago

Some references

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()) ;
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
try {
        Date date = dateformat.parse("2015-08-10T21:00:00.090Z");
        String result = new SimpleDateFormat("MM dd", Locale.getDefault()).format(cal.getTime());

} catch (ParseException e) {

Hope it helps

Saturday, August 21, 2021
answered 4 Months ago

Your Code works - as long as your System has a Locale where SEP = September. You could set the Locale to be sure about that:

Date result = new SimpleDateFormat("ddMMMyyyy HH:mm",Locale.ENGLISH).parse(dateString);
Thursday, September 2, 2021
answered 3 Months ago

I figured out how to use the global ValidationPipe with a Date property and the @IsDate() annotation:

The first step is to allow transformations like this (my bootstrap file as an example):

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe({transform: true}));
  await app.listen(3000);

Then you need to annotate the DTO with the @Type() annotation:

import { IsDate } from 'class-validator';
import { Type } from 'class-transformer';

class PersonDto {
   readonly name: string;
   @Type(() => Date)
   readonly birthDate: Date;
Wednesday, November 10, 2021
Rob N
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :