java - Generate date before certain time? -
my input string "mmyy" date format, want able replace string random date same format "mmyy" year has before 2010. should do? suggestion? have set simpledateformat?
example: input: "0914" , output should random , return string "0802" "02" 2002 before 2010.
thanks
try following snippet java 8 version:
// generate random date random random = new random(); localdate maxdate = localdate.of(2010, 1, 1); long randomday = random.nextint((int) maxdate.toepochday()); localdate randomdate = localdate.ofepochday(randomday); // convert string using 'mmyy' pattern datetimeformatter dtf = datetimeformatter.ofpattern("mmyy"); string result = dtf.format(randomdate.atstartofday()); system.out.println(result);
note 1: should generate random date between 1jan1970 , 1jan2010 (excluding) - wanted?
note 2: date format fixed , known priori way stated it. there no need input string "replace", use result
(unless misunderstood?)
note 3: see my answer more general (and possibly duplicate) question. you'll find pre-java 8 ideas there.
Comments
Post a Comment