Java Date Format formatDate(int day, int month, int year)

Here you can find the source of formatDate(int day, int month, int year)

Description

format Date

License

Apache License

Declaration

public static Date formatDate(int day, int month, int year) 

Method Source Code


//package com.java2s;
/*/*  w  ww . j  a va2 s .  co m*/
 * Copyright 2016 Pelayo Jose Lluna Gonzalez.
 *
 * Licensed 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String DATE = "dd.MM.yyyy";
    private static final String TIME = "hh:mm:ss";

    public static Date formatDate(int day, int month, int year) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE);
        final StringBuilder sb = new StringBuilder();
        Date date = new Date();

        sb.append(String.valueOf(day) + "-");
        sb.append(String.valueOf(month) + "-");
        sb.append(String.valueOf(year));

        try {
            date = dateFormat.parse(sb.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return date;
    }

    public static Date formatDate(int day, int month, int year, int hour, int minute, int second) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE + " " + TIME);
        final StringBuilder sb = new StringBuilder();
        Date date = new Date();

        sb.append(String.valueOf(day) + "-");
        sb.append(String.valueOf(month) + "-");
        sb.append(String.valueOf(year) + " ");
        sb.append(String.valueOf(hour) + ":");
        sb.append(String.valueOf(minute) + ":");
        sb.append(String.valueOf(second));

        try {
            date = dateFormat.parse(sb.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return date;
    }
}

Related

  1. formatDate(final Date date)
  2. formatDate(final Date date)
  3. formatDate(final Date date)
  4. formatDate(final java.util.Date date)
  5. formatDate(final java.util.Date date, final Supplier formatSupplier)
  6. formatDate(Integer day, Integer month, Integer year)
  7. formatDate(java.util.Calendar in)
  8. formatDate(java.util.Date date)
  9. formatDate(java.util.Date paramDate)