简单日期格式的星期几显示同一天 [英] Simple date format day of week displays same day

查看:76
本文介绍了简单日期格式的星期几显示同一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码中遇到问题,而不考虑日期,星期四显示为dayOfTheWeek.有什么想法让我错了吗?

I'm having issues with the below code displays Thursday as the dayOfTheWeek regardless of the date. Any ideas where I've gone wrong with this?

public void CreatePlan() {
    editPlanName = findViewById(R.id.editPlanName);
    String plan_name = editPlanName.getText().toString();
    DatabaseManager db;
    int day = datepicker.getDayOfMonth();
    int month = datepicker.getMonth();
    int year = datepicker.getYear();
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Integer d_name = day;
    Integer plan_recipe = 0;
    Log.d("Date", String.valueOf(d_name));
    String dayOfTheWeek = sdf.format(d_name);
    String date = day + "/" + month + "/" +year;
    db = new DatabaseManager(getApplicationContext());
    Log.d("Recipe name", recipe_name);
    db.createPlanRecipe(d_name, date, dayOfTheWeek, recipe_name);
    db.createPlan(plan_name, plan_recipe);
}

推荐答案

您必须从您的 datepicker 创建 Date ,然后将其格式化以查找如下日期:

You have to create Date from your datepicker, then format it to find day like below:

  int day = datePicker.getDayOfMonth();
  int month = datePicker.getMonth();
  int year =  datePicker.getYear();

  Calendar calendar = Calendar.getInstance();
  calendar.set(year, month - 1, day);

  SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
  dayOfTheWeek = sdf.format(calendar.getTime());

这篇关于简单日期格式的星期几显示同一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆