Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package util; /** * Author: Smitha Gudur * FileName: MyUtils.java * It provides methods for calendar */ import java.io.*; import java.util.Date; import java.util.Calendar; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class MyUtils { protected final Log logger = LogFactory.getLog(getClass()); /** * This method returns columns names * @return Vector */ public static String getDate() { Calendar cal = Calendar.getInstance(); Integer gMonth = cal.get(Calendar.MONTH) + 1; Integer gDay = cal.get(Calendar.DAY_OF_MONTH); Integer gYear = cal.get(Calendar.YEAR); String gDate = gYear.toString() + "-" + gMonth.toString() + "-" + gDay.toString(); return gDate; } // dob in database is represented as 1965-01-23 public static String getYear(String dob) { return (dob.substring(0, 4)); } // month user has entered. public static String getMonthNum(String dob) { return (dob.substring(5, 7)); } public static String getMonth(String dob) { Integer month = new Integer(dob.substring(5, 7)); return (getMonthString(month)); } /* Jan 25, 2006 converted to 2006-01-25 format */ public static String getStringDate(String date) { String year = date.substring(8, 12); String month = getRbMonth(date.substring(0, 3)); String day = date.substring(4, 6); return (year + "-" + month + "-" + day); } private static String getRbMonth(String month) { if (month.equals("Jan")) { return ("01"); } if (month.equals("Feb")) { return ("02"); } if (month.equals("Mar")) { return ("03"); } if (month.equals("Apr")) { return ("04"); } if (month.equals("May")) { return ("05"); } if (month.equals("Jun")) { return ("06"); } if (month.equals("Jul")) { return ("07"); } if (month.equals("Aug")) { return ("08"); } if (month.equals("Sep")) { return ("09"); } if (month.equals("Oct")) { return ("10"); } if (month.equals("Nov")) { return ("11"); } if (month.equals("Dec")) { return ("12"); } return null; } public static String getMonthString(int month) { switch (month - 1) { case Calendar.JANUARY: return "Jan"; case Calendar.FEBRUARY: return "Feb"; case Calendar.MARCH: return "Mar"; case Calendar.APRIL: return "Apr"; case Calendar.MAY: return "May"; case Calendar.JUNE: return "Jun"; case Calendar.JULY: return "Jul"; case Calendar.AUGUST: return "Aug"; case Calendar.SEPTEMBER: return "Sep"; case Calendar.OCTOBER: return "Oct"; case Calendar.NOVEMBER: return "Nov"; case Calendar.DECEMBER: return "Dec"; } return ""; } public static String getDay(String dob) { return (dob.substring(8, dob.length())); } public static String getWeekDay(String dob) { // sun - 1, mon (2), tue(3), wed(4), thu (5), fri(6) int day = new Integer(dob.substring(8, dob.length())).intValue(); Calendar cal = Calendar.getInstance(); cal.set(new Integer(getYear(dob)).intValue(), new Integer(getMonthNum(dob)).intValue(), day); int weekDay = cal.get(Calendar.DAY_OF_WEEK); switch (weekDay) { case Calendar.SUNDAY: return "Sunday"; case Calendar.MONDAY: return "Monday"; case Calendar.TUESDAY: return "Tuesday"; case Calendar.WEDNESDAY: return "Wednesday"; case Calendar.THURSDAY: return "Thursday"; case Calendar.FRIDAY: return "Friday"; case Calendar.SATURDAY: return "Saturday"; } return ""; } public static String getFullDate() { Calendar cal = Calendar.getInstance(); Integer gYear = cal.get(Calendar.YEAR); Integer gMonth = cal.get(Calendar.MONTH) + 1; Integer gDay = cal.get(Calendar.DAY_OF_MONTH); Integer gHour = cal.get(Calendar.HOUR_OF_DAY); Integer gMin = cal.get(Calendar.MINUTE); Integer gSec = cal.get(Calendar.SECOND); String gDate = gYear.toString() + "-" + gMonth.toString() + "-" + gDay.toString() + " " + gHour + ":" + gMin + ":" + gSec; return gDate; } }