丄諦啲仇魜ヤ
          如 果 敵 人 讓 你 生 氣 , 那 說 明 你 沒 有 勝 他 的 把 握!
          posts - 6,comments - 56,trackbacks - 1
           轉:http://www.aygfsteel.com/jakin/archive/2008/01/08/173736.html
          posted on 2008-01-08 23:27 Crying 閱讀(1064) 評論(3)  編輯  收藏 所屬分類: JAVA基礎

          FeedBack:
          # re: Java中對日期的常用處理
          2008-03-10 17:52 | Crying
          //將字符轉化為日期DATE

          String birth = addStudentForm.getBirthdayYear() + "-"+ addStudentForm.getBirthdayMonth() + "-"+ addStudentForm.getBirhthdayDay();
          try {
          SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
          Date date = sd.parse(birth);
          student.setBirthday(date);
          } catch (ParseException e) {

          System.out.println("插入失敗插入失敗插入失敗插入失敗插入失敗插入失敗");
          }
          ////將日期轉化為字符串
          SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
          String dd=sd.format(new Date());


          ////將日期轉化為Timestamp

          public static Timestamp convertDateToTimestamp(Date d)
          {
          if(d == null)
          return null;
          else
          return new Timestamp(d.getTime());
          }

          ------------------------------------------------------------
          //第二天
          public static Date getNextDate(Date date)
          {
          return getDateAddHour(date, 24);
          }

          public static Date getDateAddHour(Date date, int hour)
          {
          long l = date.getTime();
          long n = l + (long)(hour * 60 * 60 * 1000);
          Date dateAddHour = new Date(n);
          return dateAddHour;
          }
          //前一天
          public static Date getPreviousDate(Date date)
          {
          return getDateAddHour(date, -24);
          }

          注解: 加一天 就24 建一天就-24
          以此類推 2天.....



            回復  更多評論
            
          # re: Java中對日期的常用處理
          2008-11-05 11:30 | Crying
          判斷新記錄

          private boolean isNewRecord(java.sql.Timestamp date) {
          long part = (System.currentTimeMillis() - date.getTime()) / (60 * 60 * 24 * 1000);
          return (part >= 0 && part < 8);
          }  回復  更多評論
            
          # re: Java中對日期的常用處理
          2008-11-05 11:31 | Crying
          package com.ants.env.util;


          import java.sql.Timestamp;

          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          import java.util.Date;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          import com.ants.env.finance.implement.ChPlKindImp;

          // Referenced classes of package com.ants.util:
          // StringUtil

          public class DateUtil
          {

          public DateUtil()
          {
          }

          public static Date convertStrToDate(String s)
          {
          return convertStrToDate(s, "yyyy-MM-dd");
          }

          public static Date convertStrToDate(String s, String pattern)
          {
          try
          {
          SimpleDateFormat sdf = new SimpleDateFormat(pattern);
          return sdf.parse(s);
          }
          catch(ParseException pe)
          {
          pe.printStackTrace();
          }
          return null;
          }

          public static Timestamp convertStrToTimestamp(String s, String pattern)
          {
          try
          {
          SimpleDateFormat sdf = new SimpleDateFormat(pattern);
          return new Timestamp(sdf.parse(s).getTime());
          }
          catch(ParseException pe)
          {
          pe.printStackTrace();
          }
          return null;
          }

          public static java.sql.Date convertStrToSQLDate(String s)
          {
          if(StringUtil.removeNull(s).equals(""))
          return null;
          else
          return new java.sql.Date(convertStrToDate(s).getTime());
          }

          public static java.sql.Date convertStrToSQLDate()
          {
          return new java.sql.Date((new Date()).getTime());
          }

          public static java.sql.Date convertStrToSQLDate(Date date)
          {
          return new java.sql.Date(date.getTime());
          }

          public static Timestamp convertStrToTimestamp(String s)
          {
          if(StringUtil.removeNull(s).equals(s))
          return null;
          else
          return new Timestamp(convertStrToDate(s).getTime());
          }

          public static Timestamp convertStrToTimestamp(Date d)
          {
          if(d == null)
          return null;
          else
          return new Timestamp(d.getTime());
          }

          public static String getDateString()
          {
          Calendar c = Calendar.getInstance();
          Date d = c.getTime();
          return getDateString(d, "yyyy-MM-dd");
          }

          public static String getDateString(Date d)
          {
          return getDateString(d, "yyyy-MM-dd");
          }

          public static String getGeneralDateString(Date d)
          {
          return getDateString(d, "yyyy-MM-dd HH:mm:ss");
          }

          public static String getDateString(Date d, String pattern)
          {
          SimpleDateFormat sdf = new SimpleDateFormat(pattern);
          return sdf.format(d);
          }

          public static boolean equals(Date d)
          {
          return equals(d, new Date());
          }

          public static boolean equals(Date d1, Date d2)
          {
          return getDateString(d1).equals(getDateString(d2));
          }

          public static java.sql.Date getSQLDate(int year, int month, int date, int hour, int minute, int second)
          {
          _logger.info("getSQLDate:" + getDate(year, month, date, hour, minute, second).getTime());
          java.sql.Date d = new java.sql.Date(getDate(year, month, date, hour, minute, second).getTime());
          _logger.info("second:" + String.valueOf(getSecond(d)));
          return new java.sql.Date(getDate(year, month, date, hour, minute, second).getTime());
          }

          public static Date getDate(int year, int month, int date, int hour, int minute, int second)
          {
          Calendar c = Calendar.getInstance();
          c.set(year, month, date, hour, minute, second);
          _logger.info("Calendar :" + c.getTime());
          return c.getTime();
          }

          public static int getYear(Date d)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c.get(1);
          }

          public static int getMonth(Date d)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c.get(2) + 1;
          }

          public static int getDay(Date d)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c.get(5);
          }

          public static int getHour(Date d)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c.get(11);
          }

          public static int getMinute(Date d)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c.get(12);
          }

          public static int getSecond(Date d)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c.get(13);
          }

          public static Timestamp convertDateToTimestamp(Date d)
          {
          return new Timestamp(d.getTime());
          }

          public static java.sql.Date getNextMonStartDate(String year, String month)
          {
          int yearInt = Integer.parseInt(year);
          int monthInt = Integer.parseInt(month);
          if(monthInt == 12)
          return convertStrToSQLDate(Integer.toString(yearInt + 1) + "-1-1");
          else
          return convertStrToSQLDate(year + "-" + Integer.toString(monthInt + 1) + "-1");
          }

          public static java.sql.Date getMonStartDate(String year, String month)
          {
          return convertStrToSQLDate(year + "-" + month + "-1");
          }

          public static java.sql.Date getYearStartDate(String year)
          {
          return convertStrToSQLDate(year + "-1-1");
          }

          public static Date getDateAddHour(Date date, int hour)
          {
          long l = date.getTime();
          long n = l + (long)(hour * 60 * 60 * 1000);
          Date dateAddHour = new Date(n);
          return dateAddHour;
          }

          public static Date getNextDate(Date date)
          {
          return getDateAddHour(date, 24);
          }

          public static Date getSecondDate(Date date)
          {
          return getDateAddHour(date, 48);
          }

          public static Date getPreviousDate(Date date)
          {
          return getDateAddHour(date, -24);
          }

          public static Date getSQLDateAddHour(java.sql.Date date, int hour)
          {
          long l = date.getTime();
          long n = l + (long)(hour * 60 * 60 * 1000);
          java.sql.Date dateAddHour = new java.sql.Date(n);
          return dateAddHour;
          }

          public static java.sql.Date getSQLDateAddYear(java.sql.Date date, int year)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(date);
          c.set(1, c.get(1) + year);
          return new java.sql.Date(c.getTime().getTime());
          }

          public static Date getNextSQLDate(java.sql.Date date)
          {
          return getSQLDateAddHour(date, 24);
          }

          public static Date getPreviousSQLDate(java.sql.Date date)
          {
          return getSQLDateAddHour(date, -24);
          }

          public static String getStartByAllWeek(String year, int weekCount)
          {
          Calendar cal = Calendar.getInstance();
          cal.set(Integer.parseInt(year), 0, 1);
          int week = cal.get(7);
          cal.add(6, (9 - week) + (weekCount - 1) * 7);
          return new String(cal.get(1) + "-" + (cal.get(2) + 1) + "-" + cal.get(5));
          }

          public static String getStartByFirstDay(String year, int weekCount)
          {
          return getStartByAllWeek(year, weekCount - 1);
          }

          public static java.sql.Date getDateByYearQuarter(String year, String quarter)
          {
          if(quarter.equals("1"))
          return convertStrToSQLDate(year + "-01-01");
          if(quarter.equals("2"))
          return convertStrToSQLDate(year + "-04-01");
          if(quarter.equals("3"))
          return convertStrToSQLDate(year + "-07-01");
          if(quarter.equals("4"))
          return convertStrToSQLDate(year + "-10-01");
          else
          return null;
          }

          public static String[] getYearlist(int yearCount)
          {
          return getYearlist(yearCount, 0);
          }

          public static String[] getYearlist(int yearCount, int addCount)
          {
          String yearList[] = new String[yearCount];
          Date date = new Date();
          String select = "";
          int year = getYear(date) - yearCount / 2;
          for(int i = 0; i < yearCount; i++)
          {
          if(i == yearCount / 2 + addCount)
          select = "selected";
          yearList[i] = "<option " + select + ">" + year + "</option>";
          select = "";
          year++;
          }

          return yearList;
          }

          public static String getQuarterName(String quarterId)
          {
          if(quarterId.equals("1"))
          return "\u4E00\u5B63\u5EA6";
          if(quarterId.equals("2"))
          return "\u4E8C\u5B63\u5EA6";
          if(quarterId.equals("3"))
          return "\u4E09\u5B63\u5EA6";
          if(quarterId.equals("4"))
          return "\u56DB\u5B63\u5EA6";
          if(quarterId.equals("0"))
          return "\u5168\u5E74";
          else
          return "";
          }

          public static String getWeekName(Date date)
          {
          Calendar c = Calendar.getInstance();
          c.setTime(date);
          int i = c.get(7);
          if(i == 1)
          return "\u661F\u671F\u5929";
          if(i == 2)
          return "\u661F\u671F\u4E00";
          if(i == 3)
          return "\u661F\u671F\u4E8C";
          if(i == 4)
          return "\u661F\u671F\u4E09";
          if(i == 5)
          return "\u661F\u671F\u56DB";
          if(i == 6)
          return "\u661F\u671F\u4E94";
          if(i == 7)
          return "\u661F\u671F\u516D";
          else
          return "";
          }

          public static String getWeekName()
          {
          return getWeekName(new Date());
          }

          public static boolean isInTime(Timestamp ts, int hour)
          {
          long todayTime = (new Date()).getTime();
          return ts.getTime() <= todayTime + (long)(hour * 60 * 60 * 1000) && ts.getTime() >= todayTime;
          }

          public static void main(String args[])
          {
          System.out.println(convertStrToTimestamp("2005-06-06 14-05-06", "yyyy-MM-dd HH-mm-ss"));
          }

          public static String getHourSelect(String hour)
          {
          String hourSelect = "";
          hour = StringUtil.removeNull(hour);
          if(!"".equals(hour))
          hourSelect = "<option value=" + StringUtil.addZero(hour) + " selected>" + StringUtil.addZero(hour) + "</option>";
          for(int i = 0; i < 24; i++)
          hourSelect = hourSelect + "<option value=" + StringUtil.addZero(i) + " >" + StringUtil.addZero(i) + "</option>";

          return hourSelect;
          }

          public static String getMinuteAndSecondSelect(String MinuteOrSecond)
          {
          String MinuteOrSecondSelect = "";
          MinuteOrSecond = StringUtil.removeNull(MinuteOrSecond);
          if(!"".equals(MinuteOrSecond))
          MinuteOrSecondSelect = "<option value=" + StringUtil.addZero(MinuteOrSecond) + " selected>" + StringUtil.addZero(MinuteOrSecond) + "</option>";
          for(int i = 0; i < 60; i++)
          MinuteOrSecondSelect = MinuteOrSecondSelect + "<option value=" + StringUtil.addZero(i) + " >" + StringUtil.addZero(i) + "</option>";

          return MinuteOrSecondSelect;
          }

          // private static Logger _logger;
          // static Class class$0; /* synthetic field */



          private static final Log _logger = LogFactory.getLog(ChPlKindImp.class);
          }

            回復  更多評論
            
          主站蜘蛛池模板: 阳泉市| 子洲县| 江门市| 逊克县| 始兴县| 射阳县| 石阡县| 拉孜县| 阜康市| 大足县| 建德市| 喀什市| 襄垣县| 浠水县| 左权县| 广安市| 乌兰察布市| 达尔| 小金县| 托克托县| 洞口县| 西丰县| 东乡县| 中卫市| 利辛县| 镇雄县| 曲麻莱县| 东丽区| 清原| 怀安县| 双鸭山市| 太和县| 葫芦岛市| 息烽县| 甘洛县| 汝南县| 泌阳县| 邮箱| 大宁县| 昭通市| 上犹县|