Java Timestamp Field getRandTimestamp(Random r)

Here you can find the source of getRandTimestamp(Random r)

Description

get Rand Timestamp

License

Apache License

Declaration

public static Timestamp getRandTimestamp(Random r) 

Method Source Code

//package com.java2s;
/**/*from   ww w .  j a  va2s. c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.Timestamp;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * TIMESTAMP.
     */

    public static final long NANOSECONDS_PER_SECOND = TimeUnit.SECONDS.toNanos(1);
    public static final long MILLISECONDS_PER_SECOND = TimeUnit.SECONDS.toMillis(1);
    public static final long NANOSECONDS_PER_MILLISSECOND = TimeUnit.MILLISECONDS.toNanos(1);
    public static int MIN_YEAR = 1900;
    public static int MAX_YEAR = 3000;

    public static Timestamp getRandTimestamp(Random r) {
        return getRandTimestamp(r, MIN_YEAR, MAX_YEAR);
    }

    public static Timestamp getRandTimestamp(Random r, int minYear, int maxYear) {
        String optionalNanos = "";
        switch (r.nextInt(4)) {
        case 0:
            // No nanos.
            break;
        case 1:
            optionalNanos = String.format(".%09d", Integer.valueOf(r.nextInt((int) NANOSECONDS_PER_SECOND)));
            break;
        case 2:
            // Limit to milliseconds only...
            optionalNanos = String.format(".%09d",
                    Integer.valueOf(r.nextInt((int) MILLISECONDS_PER_SECOND)) * NANOSECONDS_PER_MILLISSECOND);
            break;
        case 3:
            // Limit to below milliseconds only...
            optionalNanos = String.format(".%09d", Integer.valueOf(r.nextInt((int) NANOSECONDS_PER_MILLISSECOND)));
            break;
        }
        String timestampStr = String.format("%04d-%02d-%02d %02d:%02d:%02d%s",
                Integer.valueOf(minYear + r.nextInt(maxYear - minYear + 1)), // year
                Integer.valueOf(1 + r.nextInt(12)), // month
                Integer.valueOf(1 + r.nextInt(28)), // day
                Integer.valueOf(0 + r.nextInt(24)), // hour
                Integer.valueOf(0 + r.nextInt(60)), // minute
                Integer.valueOf(0 + r.nextInt(60)), // second
                optionalNanos);
        Timestamp timestampVal;
        try {
            timestampVal = Timestamp.valueOf(timestampStr);
        } catch (Exception e) {
            System.err.println("Timestamp string " + timestampStr + " did not parse");
            throw e;
        }
        return timestampVal;
    }
}

Related

  1. getOffsetDaysTime(Timestamp sysDate, int offsetDays)
  2. getPerformanceTime(Timestamp startTime)
  3. getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration)
  4. getPresentTimeStamp()
  5. getRandomTimeBetweenTwoDates(Timestamp beginDate, Map context)
  6. getRealCalendar(Timestamp time)
  7. getResultRowValueAsTimestamp(String valueName, Map resultRow)
  8. getSafeLong(final Timestamp d, final long i)
  9. getSeconds(Timestamp timeStamp)