Java Calendar Create getCalendar(String dateStr, int inputYearType, int outputYearType)

Here you can find the source of getCalendar(String dateStr, int inputYearType, int outputYearType)

Description

get Calendar

License

Open Source License

Parameter

Parameter Description

Return

Calendar

Declaration

public static Calendar getCalendar(String dateStr, int inputYearType, int outputYearType) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.StringTokenizer;
import java.util.Vector;

public class Main {
    final public static int EN = 1;
    final public static int TH = 2;

    /**// w w  w . j a  v  a2  s  .  c o m
     * @param String("dd/MM/yyyy)
     * @return Calendar 
     */
    public static Calendar getCalendar(String dateStr, int inputYearType, int outputYearType) {
        // date (dd/MM/yyyy)
        Vector d = getElementDate(dateStr);

        int day = Integer.parseInt((String) d.elementAt(0));
        int month = Integer.parseInt((String) d.elementAt(1));
        int year = Integer.parseInt((String) d.elementAt(2));

        switch (inputYearType) {
        case EN:
            if (outputYearType == TH)
                year += 543;
            break;
        case TH:
            if (outputYearType == EN)
                year -= 543;
            break;
        }

        return new GregorianCalendar(year, month - 1, day); //MUST SUB 1, BECAUSE MONTH START FROM 0
    }

    /**
     * @param String("dd/MM/yyyy)
     * @return int
     */
    public static Vector getElementDate(String dateStr) {
        Vector vec = new Vector();
        StringTokenizer stk = new StringTokenizer(dateStr, "/");
        while (stk.hasMoreElements())
            vec.addElement((String) stk.nextElement());
        return vec;
    }
}

Related

  1. getCalendar(long millis)
  2. getCalendar(long time)
  3. getCalendar(long timeInMillis)
  4. getCalendar(long timeInMillis)
  5. getCalendar(String date)
  6. getCalendar(String dateString)
  7. getCalendar(String gdate)
  8. getCalendar(String str)
  9. getCalendar(String time, char sep, boolean isStart)