【问题标题】:How to get date automatically through a button如何通过按钮自动获取日期
【发布时间】:2016-01-03 12:06:41
【问题描述】:

我想知道如何通过 Android Studio 中的 Android 应用通过按钮自动检索日期。

【问题讨论】:

  • 您希望日期采用什么格式?日期对象?字符串?

标签: android date android-studio automatic-storage


【解决方案1】:

在你的 onClick 按钮监听方法中添加:

Time today = new Time(Time.getCurrentTimezone());
today.setToNow();

【讨论】:

    【解决方案2】:

    也许这可以帮助你:

       yourButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Create Date Object
                    Date today = new Date();
    
                    //Convert to calendar Object
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(today);
    
                    int year = calendar.get(Calendar.YEAR);
                    int month = calendar.get(Calendar.MONTH);
                    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                    int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
                    int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
                  }
            });
    

    【讨论】:

    • 我想知道这是否需要包含在main_activity.java中
    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2011-11-22
    • 2016-03-03
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多