0

Now I am parsing datetime from frontend like this:

public static Long stringToUnixTimestampMinisecend(String datetime) {
        Long resultDatetime = 0L;
        try {
            Date startLocalTime = DateUtils.parseDate(datetime, DATETIME_PATTERN);
            resultDatetime = startLocalTime.getTime();
        } catch (Exception e) {
            Log.error("parse error", e);
        }
        return resultDatetime;
    }

this is the DATETIME_PATTERN define:

 public final static String[] DATETIME_PATTERN = {
            "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM-dd",
    };

but now I want to know the frontend parameter is contain minisecond, if the format like this 2020-11-05 23:59:59, then I could append 999 in the end of parsed date(default is 000). what should I do to make this?

Dolphin
  • 29,069
  • 61
  • 260
  • 539

1 Answers1

2

You can use this pattern

  "yyyy-MM-dd HH:mm:ss.SSS",

If you want to add milliseconds at the end, I'll give you an example

Date date = new Date();

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.999");
System.out.println("Date: " + sdfDate.format(date)); 
borchvm
  • 3,533
  • 16
  • 44
  • 45
  • 1
    but the input from parameter is `2020-11-05 23:59:59`, I want to append 999 in the end of time. @borchvm – Dolphin Nov 06 '20 at 10:34