Java Date Parse getDateFromDateAge(Date currentDate, String age)

Here you can find the source of getDateFromDateAge(Date currentDate, String age)

Description

Retrieves the original date by current date and an age.

License

Open Source License

Parameter

Parameter Description
currentDate a parameter
age a parameter

Declaration

public static Date getDateFromDateAge(Date currentDate, String age) 

Method Source Code

//package com.java2s;
/*// ww  w.  j a  va2 s  . com
 * Author Stephen Booysen
 *
 * Copyright (c) 2015 Stephen Booysen, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Stephen Booysen. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Stephen Booysen
 *
 * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**
     * Retrieves the original date by current date and an age.
     * 
     * @param currentDate
     * @param age
     * @return
     */
    public static Date getDateFromDateAge(Date currentDate, String age) {
        Calendar cal = Calendar.getInstance();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String nowString = sdf.format(currentDate);
            cal.setTime(sdf.parse(nowString));
        } catch (ParseException e) {
            cal.setTime(currentDate);
        }
        if (age.contains("m") || age.contains("M")) {
            age = age.replace("m", "");
            age = age.replace("M", "");
            int months = Integer.parseInt(age) * -1;
            cal.add(Calendar.MONTH, months);
        } else {
            double years = Double.parseDouble(age) * -1;
            cal.add(Calendar.YEAR, (int) years);
        }
        return cal.getTime();
    }

    /**
     * Set the time of the given Date
     *
     * @param date
     * @param hourOfDay
     * @param minute
     * @param second
     * @param ms
     *
     * @return new instance of java.util.Date with the time set
     */
    public static Date setTime(final Date date, final int hourOfDay, final int minute, final int second,
            final int ms) {
        final GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.set(Calendar.HOUR_OF_DAY, hourOfDay);
        gc.set(Calendar.MINUTE, minute);
        gc.set(Calendar.SECOND, second);
        gc.set(Calendar.MILLISECOND, ms);
        return gc.getTime();
    }

    /**
     * Try and evaluate the string and return a date
     * 
     * @param input
     * @return
     */
    public static Date parse(String input) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy/MM/dd").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("dd MMM yyyy").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy").parse(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(input);
        } catch (Exception e) {

        }
        try {
            return new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(input);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Try to evaluate a date and return a string
     * 
     * @param input
     * @return
     */
    public static String parse(Date input) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(input);
        } catch (Exception e) {
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(input);
        } catch (Exception e) {
        }

        return null;
    }

    /**
     * Try to evaluate a date and return a string
     * 
     * @param input
     * @return
     */
    public static String parse(Date input, String format) {

        try {
            return new SimpleDateFormat(format).format(input);

        } catch (Exception e) {
        }

        return null;
    }
}

Related

  1. datetime(String s)
  2. dateTime(String s)
  3. datetimeParse(String datetime)
  4. DateTimeParse(String DateTimeString, String LMS)
  5. getDateFromCurrentAnd24HRTime(Date current, String hr24Time)
  6. getDateFromDatestamp(String datestamp)
  7. getDateFromDateTimeUTCFormat(final String dateString)
  8. getDateFromDCItemTimestamp(String dateTimeStamp)
  9. getDateFromFileName(String fileName)