Java date formatting?

后端 未结 4 539
深忆病人
深忆病人 2021-01-19 16:40

I want to read a date in the format YYYY-MM-DD.

But if I enter date say for example 2008-1-1, I want to read it as 2008-01-01.

Can anybody help me? Thanks

4条回答
  •  太阳男子
    2021-01-19 17:25

    import java.text.*;
    
    public class Test
    {
    
        public static void main(String args[])
        {
            try
            {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");  
                System.out.println(sdf.parse(args[0]).toString());
            }
                    catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    

    This works OK, no matter if you write as argument "2008-1-1" or "2008-01-01".

提交回复
热议问题