Java random generation time string method, java generation string

Source: Internet
Author: User

Java random generation time string method, java generation string

The examples in this article share with you the specific code of the java random generation time string for your reference. The specific content is as follows:

Package com. wechat. utils; import java. text. simpleDateFormat; import java. util. date;/*** Created by hexun on instances /2/4. */public class RandTimeUtils {/*** generate random time * @ param beginDate * @ param endDate * @ return */private static Date randomDate (String beginDate, String endDate) {try {SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd"); Date start = format. parse (beginDate); // construct the start Date en D = format. parse (endDate); // construct the end Date // getTime () indicates the number of milliseconds that the Date object represents since 00:00:00 GMT, January 1, January 1, 1970. If (start. getTime ()> = end. getTime () {return null;} long date = random (start. getTime (), end. getTime (); return new Date (date);} catch (Exception e) {e. printStackTrace ();} return null;} private static long random (long begin, long end) {long rtn = begin + (long) (Math. random () * (end-begin); // If the returned start time and end time are, call this function recursively to find the random value if (rtn = begin | rtn = end) {return random (begin, end) ;}return rtn ;} public static void main (String [] args) {Date randomDate = randomDate ("2010-09-20", "2017-02-04"); SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); String resulttime = format. format (randomDate); // construct the start date System. out. println (resulttime );}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.