【问题标题】:get current date and time获取当前日期和时间
【发布时间】:2012-12-04 21:51:01
【问题描述】:

我有点卡在日期和时间上。 我希望我的程序创建像“20121217”这样的日期。前 4 个字母是年份,后 2 个字母是月份,最后 2 个字母是日期。年+月+日

时间为“112233”时+分+秒

感谢您的帮助!

【问题讨论】:

    标签: android


    【解决方案1】:

    这是格式问题。 Java 使用 java.util.Datejava.text.DateFormatjava.text.SimpleDateFormat 来处理这些事情。

    DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd hhmmss");
    dateFormatter.setLenient(false);
    Date today = new Date();
    String s = dateFormatter.format(today);
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      Calendar c = Calendar.getInstance();
      String date = c.get(Calendar.YEAR) + c.get(Calendar.MONTH) + c.get(Calendar.DATE);
      String time = c.get(Calendar.HOUR) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);
      

      【讨论】:

        【解决方案3】:

        根据需要更改任何特定的时间或日期格式。

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
             currentDateandTime = sdf.format(new Date());
        

        【讨论】:

          【解决方案4】:

          日期:

          DateFormat df = new SimpleDateFormat("yyyyMMdd");
          String strDate = df.format(new Date());
          

          时间:

          DateFormat df = new SimpleDateFormat("hhmmss");
          String strTime = df.format(new Date());
          

          【讨论】:

            【解决方案5】:

            这行得通,

             String currentDateTime;
            
             SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             currentDateTime = sdf1.format(new Date());
            

            【讨论】:

              【解决方案6】:

              您正在寻找的是 Java 中的 SimpleDateFormat...看看this page

              根据您的需要试试这个:

              SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss");
              Date parsed = format.parse(new Date());
              System.out.println(parsed.toString());
              

              【讨论】:

                【解决方案7】:

                您可以使用以下方法获取当前时间

                 /**************************************************************
                 * getCurrentTime() it will return system time
                 * 
                 * @return
                 ****************************************************************/
                public static String getCurrentTime() {     
                    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
                    Calendar cal = Calendar.getInstance();
                    return dateFormat.format(cal.getTime());
                }// end of getCurrentTime()
                

                【讨论】:

                  猜你喜欢
                  • 2015-01-18
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-03-22
                  • 2011-01-01
                  • 2011-10-18
                  • 2018-09-26
                  • 2012-04-29
                  相关资源
                  最近更新 更多