Java Date Create buildDate(int y, int m, int d)

Here you can find the source of buildDate(int y, int m, int d)

Description

build Date

License

Artistic License

Declaration

public static Date buildDate(int y, int m, int d) 

Method Source Code


//package com.java2s;
//License from project: Artistic License 

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

public class Main {

    public static Date buildDate(int y, int m, int d) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, y);
        cal.set(Calendar.MONTH, m - 1);
        cal.set(Calendar.DAY_OF_MONTH, d);
        return cal.getTime();
    }//www  .  ja va2  s  .  c o  m

    public static Date buildDate(int y, int m, int d, int h, int mm, int s) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, y);
        cal.set(Calendar.MONTH, m - 1);
        cal.set(Calendar.DAY_OF_MONTH, d);
        cal.set(Calendar.HOUR_OF_DAY, h);
        cal.set(Calendar.MINUTE, mm);
        cal.set(Calendar.SECOND, s);
        return cal.getTime();
    }

    public static Date buildDate(String strDate) {
        final String pattern = "yyyy-MM-dd";
        return buildDate(strDate, pattern);
    }

    public static Date buildDate(String strDate, String pattern) {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. add24HtoDate(Date date)
  2. addTimeToDate(Date date, Date time)
  3. buildDate(String dateAsString)
  4. buildDateFormat(final String pattern)
  5. buildDatetime(Date datePart, Date hourMinutePart)
  6. buildDateTime(String curDate, String curTime, String meridiem)