-1

I am new to java.I am suppose to use pattern matcher inside getter method such that e.g: getCreatedTime() which actualy has miliseconds is removed via passing it to through pattern matcher inside getter.

RequestTO.getDetailsTO().setReqTimeStamp(automaticalyRecoveryTO.getCreatedTime());

Currently time format is in YYYY-MM-dd HH:mm:ss.SSS i.e. 26-SEP-18 04.29.48.452000000 PM expected:- 26-SEP-18 04.29.48 PM without miliseconds or without writing a new function.Please help me with this.

itsmysterybox
  • 2,748
  • 3
  • 21
  • 26
  • `26-SEP-18 04.29.48.452000000 PM ` does not match the pattern `YYYY-MM-dd HH:mm:ss.SSS`, so which is it? – TiiJ7 Sep 27 '18 at 08:14
  • `getCreatedDate()` returns `Date` (java.util.Date ???) ... https://stackoverflow.com/questions/1908387/java-date-cut-off-time-information – xerx593 Sep 27 '18 at 08:18

2 Answers2

1
public static String getCreatedTime() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return strDate;
}
moritzg
  • 4,266
  • 3
  • 37
  • 62
0

Currently time format is in YYYY-MM-dd HH:mm:ss.SSS

Should have been YYYY-MM-dd HH:mm:ss

26-SEP-18 04.29.48.452000000 PM is the toString() of the old Date class in some English locale (PM means, the time is 16:29:48, HH=16).

To replace the milliseconds here:

String s = date.toString();
s = s.replaceFirst("(\\s\\d\\d.\\d\\d.\\d\\d)(\\d*)", "$1");

Here . stands for any char, also . itself; for other locales.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138