Java String to Date toDate(int date, String timeFormat)

Here you can find the source of toDate(int date, String timeFormat)

Description

to Date

License

Open Source License

Declaration

public static Date toDate(int date, String timeFormat) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String yyyyMMdd = "yyyyMMdd";

    public static Date toDate(int date, String timeFormat) {
        StringBuilder sbBuilder = new StringBuilder();
        if (!yyyyMMdd.equals(timeFormat) && String.valueOf(date).length() == 8) {
            sbBuilder.append("0");
        }//  w  w  w.j  ava2  s.co m
        sbBuilder.append(date);
        DateFormat sdf = new SimpleDateFormat(timeFormat);
        Date datetime = new Date();
        try {
            datetime = sdf.parse(sbBuilder.toString());
        } catch (ParseException e) {
            throw new RuntimeException("parse the time [" + date + "] with format [" + timeFormat + "] error", e);
        }
        return datetime;
    }

    public static Date toDate(int date, int time, String timeFormat) {
        StringBuilder sbBuilder = new StringBuilder();
        sbBuilder.append(date);
        if (String.valueOf(time).length() == 8) {
            sbBuilder.append("0");
        }
        sbBuilder.append(time);
        DateFormat sdf = new SimpleDateFormat(timeFormat);
        Date datetime = new Date();
        try {
            datetime = sdf.parse(sbBuilder.toString());
        } catch (ParseException e) {
            throw new RuntimeException(
                    "parse the time [" + date + " " + time + "] with format [" + timeFormat + "] error", e);
        }
        return datetime;
    }
}

Related

  1. toDate(final Long date, final String pattern)
  2. toDate(final String _text, final String _dateFormat)
  3. toDate(final String date, final String time)
  4. toDate(final String dateString, final String format)
  5. toDate(final String dateString, final String pattern)
  6. toDate(long value, String format)
  7. toDate(Map filter, String field)
  8. toDate(Object v, String format, Date defaultValue)
  9. toDate(Object value, String format)