Java Time Format getCalendarByDateTime(int dateTime, String dateFormat)

Here you can find the source of getCalendarByDateTime(int dateTime, String dateFormat)

Description

get Calendar By Date Time

License

Open Source License

Declaration

public static Calendar getCalendarByDateTime(int dateTime, String dateFormat) 

Method Source Code


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

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

    public static Calendar getCalendarByDateTime(int dateTime, String dateFormat) {

        Date date = toDate(dateTime, dateFormat);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);/*from ww w  . j a  va  2s .c  o m*/
        return calendar;
    }

    public static Date toDate(int date, String timeFormat) {
        StringBuilder sbBuilder = new StringBuilder();
        if (!yyyyMMdd.equals(timeFormat) && String.valueOf(date).length() == 8) {
            sbBuilder.append("0");
        }
        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. formatTimeUnits(double nanos)
  2. formatTimeZoneID(String id)
  3. formatToTimeStr(String str)
  4. formatUptime(long uptime)
  5. formatVideoRecordingTime(long t)
  6. getCurDateTime(String formatStr)
  7. getCurDateTimeFormat(String Format)
  8. getCurrDatetimeWithDbFormat()
  9. getCurTime(String dateformat)