我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

获取昨天的日期字符串

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());
    }


}



posted on 2008-07-11 10:19 々上善若水々 阅读(1329) 评论(0)  编辑  收藏 所属分类: J2SE


只有注册用户登录后才能发表评论。


网站导航: