欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java获取昨天日期字符串的方法

程序员文章站 2024-03-13 13:50:03
本文实例讲述了java获取昨天日期字符串的方法。分享给大家供大家参考,具体如下: import java.text.simpledateformat; impo...

本文实例讲述了java获取昨天日期字符串的方法。分享给大家供大家参考,具体如下:

import java.text.simpledateformat;
import java.util.date;
public class test
{
  /**
   * 获取昨天的日期字符串
   *
   * @param nowdate
   *      当前日期的字符串
   * @return
   */
  public static string getlastdayinfo(string nowdate)
  {
    string yesterday = "";
    int year = 0;
    int month = 0;
    int day = 0;
    try
    {
      year = integer.parseint(nowdate.substring(0, nowdate.indexof("-")));
      month = integer.parseint(nowdate.substring(
          nowdate.indexof("-") + 1, nowdate.lastindexof("-")));
      day = integer.parseint(nowdate
          .substring(nowdate.lastindexof("-") + 1));
      // system.out.println("nowyear = " + year);
      // system.out.println("nowmonth = " + month);
      // system.out.println("dayday = " + day);
      day = day - 1;
      if (day == 0)
      {
        month = month - 1;
        if (month == 0)
        {
          // january
          month = 12;
          day = 31;
          year = year - 1;
        }
        else
        {
          // not jan.
          switch (month)
          {
          // 1|3|5|7|8|10|12) day=31;;
          case 1:
            day = 31;
            break;
          case 3:
            day = 31;
            break;
          case 5:
            day = 31;
            break;
          case 7:
            day = 31;
            break;
          case 8:
            day = 31;
            break;
          case 10:
            day = 31;
            break;
          case 12:
            day = 31;
            break;
          // 4|6|9|11) day=30;;
          case 4:
            day = 30;
            break;
          case 6:
            day = 30;
            break;
          case 9:
            day = 30;
            break;
          case 11:
            day = 30;
            break;
          case 2:
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            {
              // leap year
              day = 29;
            }
            else
              day = 28;
          }
        }
      }
      string monthstr = "";
      string daystr = "";
      if (month < 10)
      {
        monthstr = "0" + string.valueof(month);
      }
      else
      {
        monthstr = string.valueof(month);
      }
      if (day < 10)
      {
        daystr = "0" + string.valueof(day);
      }
      else
      {
        daystr = string.valueof(day);
      }
      yesterday = string.valueof(year) + "-" + monthstr + "-" + daystr;
    } catch (exception e)
    {
      e.printstacktrace();
    }
    return yesterday;
  }
  public static void main(string[] args) throws exception
  {
    system.out.println(getlastdayinfo("2008-03-01"));
    system.out.println(getlastdayinfo("2008-01-01"));
    simpledateformat sdf = new simpledateformat("mmmm-dd-yyyy");
    date date;
    string datetime = "02-14-1984";
    try
    {
      date = sdf.parse(datetime);
      system.out.println(sdf.format(date));
    } catch (exception e)
    {
      system.out.println(e.getmessage());
    }
    date d = java.sql.date.valueof("2008-07-06");
    system.out.println(d.tostring());
  }
}

更多关于java相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。