【问题标题】:Get Current System Time获取当前系统时间
【发布时间】:2011-07-02 16:12:02
【问题描述】:

如何在不使用Calender.getInstance()的情况下获取当前系统的时间和日期?

【问题讨论】:

    标签: android datetime


    【解决方案1】:

    试试这个:

    //-------- Don't know whether this will work on android or not.
    long dtMili = System.currentTimeMillis();
    Date dt = new Date(dtMili);
    

    new Date().getTime()
    

    【讨论】:

    • 但它只返回像 2011-02-23 这样的日期,但我也需要像 "2011-02-23 16:38:45" 这样的日期时间
    • @CapDroid: 为此使用DateFormat 类。
    【解决方案2】:

    构造一个新的Date 将给出当前日期。此后您可以使用 date.setTime(System.currentTimeMillis()) 来更新该对象

    【讨论】:

      【解决方案3】:

      试试

      Date d = new Date();
      CharSequence s  = DateFormat.format("EEEE, MMMM d, yyyy ", d.getTime());
      

      您可以根据需要编辑格式字符串..

      【讨论】:

      • 嗨 Tushar。我需要这个格式的 DateTime(yyyy-MM-dd HH:mm:ss)
      • 你只能使用'hh'而不是'HH'
      • 它对我不起作用。 The method format(Date) in the type DateFormat is not applicable for the arguments (String, long)
      【解决方案4】:

      你可以试试这个:

        DateFormat df = DateFormat.getTimeInstance();
        df.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
      
        df.format(new Date(System.currentTimeMillis());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 2016-12-04
        • 2016-07-30
        • 2013-03-19
        • 1970-01-01
        相关资源
        最近更新 更多