質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
date

dateは、date型や日付に関する関数や処理についてのタグです

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

980閲覧

AndroidアプリでDate型の日付の受け取りがうまくいきません

Yakusugi

総合スコア123

date

dateは、date型や日付に関する関数や処理についてのタグです

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/05/04 03:08

編集2021/05/04 21:54

イメージ説明Android Studioで家計簿アプリを作成しています。
activity_main.xmlから日付の値を受け取り、dtoから日付(Date型)の値をMainActivityに渡したいのですが、MainActivityの47行目での「txtDate.getText().toString()」の部分で下記エラーが発生してしまいました。

Required type:
Date
Provided:
String

この場合、Date型をString型に変換するなどの処理が必要でしょうか。
上記エラー解決の為、ご助力頂けますと幸いです。
※まだ作成途中の為、コード自体は中途半端な状態です。

お手数ですが、ご回答の程よろしくお願いいたします。

MainActivity.java

1package com.example.budgettrackerapptest1; 2 3import androidx.annotation.RequiresApi; 4import androidx.appcompat.app.AppCompatActivity; 5import androidx.recyclerview.widget.RecyclerView; 6 7import android.os.Build; 8import android.os.Bundle; 9import android.view.View; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.Toast; 13 14import java.text.DateFormat; 15import java.text.ParseException; 16import java.text.SimpleDateFormat; 17import java.time.LocalDate; 18import java.time.format.DateTimeFormatter; 19import java.util.Date; 20import java.util.Locale; 21 22public class MainActivity extends AppCompatActivity { 23 24 Button btnViewAll, btnSelect, btnAdd, btnUpdate; 25 EditText txtDate, txtStoreName, txtProductName, txtType, txtPrice; 26 RecyclerView rv_list; 27 BudgetTrackerDao budgetTrackerDao; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 btnViewAll = findViewById(R.id.btnViewAll); 35 btnSelect = findViewById(R.id.btnSelect); 36 btnAdd = findViewById(R.id.btnAdd); 37 btnUpdate = findViewById(R.id.btnUpdate); 38 txtDate = findViewById(R.id.txtDate); 39 txtStoreName = findViewById(R.id.txtStoreName); 40 txtProductName = findViewById(R.id.txtProductName); 41 txtType = findViewById(R.id.txtType); 42 txtPrice = findViewById(R.id.txtPrice); 43 rv_list = findViewById(R.id.rv_list); 44 budgetTrackerDao = new BudgetTrackerDao(MainActivity.this); 45 46 //ShowCustomerOnListView(dataBaseHelper); need to create a new method on this class 47 48 btnAdd.setOnClickListener(new View.OnClickListener() { 49 50 @Override 51 public void onClick(View v) { 52 BudgetTrackerDto budgetTrackerDto; 53 DateFormat txtDateFormat = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); 54 Date txtDateStr = null; 55 try { 56 txtDateStr = txtDateFormat.parse(String.valueOf(txtDate)); 57 } catch (ParseException e) { 58 e.printStackTrace(); 59 } 60 61 62 try { 63 budgetTrackerDto = new BudgetTrackerDto(txtDateStr.getTime(), txtStoreName.getText().toString(), txtProductName.getText().toString(),txtType.getText().toString(), Integer.parseInt(txtPrice.getText().toString())); 64 Toast.makeText(MainActivity.this, budgetTrackerDto.toString(), Toast.LENGTH_SHORT).show(); 65 } catch (Exception e) { 66 Toast.makeText(MainActivity.this, "Error adding date", Toast.LENGTH_SHORT).show(); 67 68 } 69 //作成途中 70 71 } 72 }); 73 74 75 } 76}

BudgetTrackerDto.java

1package com.example.budgettrackerapptest1; 2 3import java.sql.Date; 4 5public class BudgetTrackerDto { 6 private Date date; 7 private String storeName; 8 private String productName; 9 private String productType; 10 private int price; 11 12 public BudgetTrackerDto (long date, String storeName, String productName, String productType, int price) { 13 this.date = date; 14 this.storeName = storeName; 15 this.productName = productName; 16 this.productType = productType; 17 this.price = price; 18 } 19 20 21 @Override 22 public String toString() { 23 return "BudgetTrackerDto{" + 24 "date=" + date + 25 ", storeName='" + storeName + '\'' + 26 ", productName='" + productName + '\'' + 27 ", productType='" + productType + '\'' + 28 ", price=" + price + 29 '}'; 30 } 31 32 public Date getDate() { 33 return date; 34 } 35 36 public void setDate(Date date) { 37 this.date = date; 38 } 39 40 public String getStoreName() { 41 return storeName; 42 } 43 44 public void setStoreName(String storeName) { 45 this.storeName = storeName; 46 } 47 48 public String getProductName() { 49 return productName; 50 } 51 52 public void setProductName(String productName) { 53 this.productName = productName; 54 } 55 56 public String getProductType() { 57 return productType; 58 } 59 60 public void setProductType(String productType) { 61 this.productType = productType; 62 } 63 64 public int getPrice() { 65 return price; 66 } 67 68 public void setPrice(int price) { 69 this.price = price; 70 } 71} 72

BudgetTrackerDao.java

1package com.example.budgettrackerapptest1; 2 3import android.content.Context; 4import android.database.sqlite.SQLiteDatabase; 5import android.database.sqlite.SQLiteOpenHelper; 6 7import androidx.annotation.Nullable; 8 9public class BudgetTrackerDao extends SQLiteOpenHelper { 10 11 public BudgetTrackerDao(@Nullable Context context) { 12 super(context, "budgetTrackerSample1.db", null, 1); 13 } 14 15 @Override 16 public void onCreate(SQLiteDatabase db) { 17 18 } 19 20 @Override 21 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 22 23 } 24}

activity_main.xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical" 13 tools:layout_editor_absoluteX="157dp" 14 tools:layout_editor_absoluteY="52dp"> 15 16 <EditText 17 android:id="@+id/txtDate" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:ems="10" 21 android:hint="Date" 22 android:inputType="date" /> 23 24 <EditText 25 android:id="@+id/txtStoreName" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:ems="10" 29 android:hint="Store Name" 30 android:inputType="textPersonName" /> 31 32 <EditText 33 android:id="@+id/txtProductName" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:ems="10" 37 android:hint="Product Name" 38 android:inputType="textPersonName" /> 39 40 <EditText 41 android:id="@+id/txtType" 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:ems="10" 45 android:hint="Product Type" 46 android:inputType="textPersonName" /> 47 48 <EditText 49 android:id="@+id/txtPrice" 50 android:layout_width="match_parent" 51 android:layout_height="wrap_content" 52 android:ems="10" 53 android:hint="Price" 54 android:inputType="number" /> 55 56 <LinearLayout 57 android:layout_width="match_parent" 58 android:layout_height="wrap_content" 59 android:orientation="horizontal"> 60 61 <Button 62 android:id="@+id/btnViewAll" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" 65 android:layout_margin="6dp" 66 android:layout_weight="1" 67 android:text="View All" /> 68 69 <Button 70 android:id="@+id/btnSelect" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:layout_margin="6dp" 74 android:layout_weight="1" 75 android:text="Select" /> 76 77 <Button 78 android:id="@+id/btnAdd" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:layout_margin="6dp" 82 android:layout_weight="1" 83 android:text="Add Item" /> 84 85 <Button 86 android:id="@+id/btnUpdate" 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 android:layout_margin="6dp" 90 android:layout_weight="1" 91 android:text="Update" /> 92 </LinearLayout> 93 94 <androidx.recyclerview.widget.RecyclerView 95 android:id="@+id/rv_list" 96 android:layout_width="match_parent" 97 android:layout_height="match_parent" /> 98 </LinearLayout> 99</androidx.constraintlayout.widget.ConstraintLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

この場合、Date型をString型に変換するなどの処理が必要でしょうか。

そういうことになります。String型の日付の書式文字列に合わせて、DateFormat#parseメソッドを使います。

DateFormat#parse - Android デベロッパー

参考: Dateクラスのコンストラクタやparseを使ってDateへ変換することはバージョンによっては非推奨なので、DateFormat#parseを使いましょう。
Date - 同上

投稿2021/05/04 03:30

dodox86

総合スコア9183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yakusugi

2021/05/04 04:40 編集

ご回答ありがとうございます。 度々すみません。 何度か修正してみたところ、 エラーは消えたのですが、下記だと問題無いでしょうか。 @Override public void onClick(View v) { BudgetTrackerDto budgetTrackerDto; DateFormat txtDateFortmat = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); Date txtDateStr = null; try { txtDateStr = txtDateFortmat.parse(String.valueOf(txtDate)); } catch (ParseException e) { e.printStackTrace(); } try { budgetTrackerDto = new BudgetTrackerDto(txtDateStr.getTime(), txtStoreName.getText().toString(), txtProductName.getText().toString(),txtType.getText().toString(), Integer.parseInt(txtPrice.getText().toString())); } //作成途中
dodox86

2021/05/04 05:37

回答は以下の疑問について言及したもので、 > この場合、Date型をString型に変換するなどの処理が必要でしょうか。 「Dateクラスにいきなり文字列を投入することはできないので、DateFormat#parseを使いましょう」ということが本旨です。 全体の動きとしては質問者さんのアプリ、日付の文字列の入力形式やエラー判定の基準、例外のハンドリングによって変わってくるはずなので、それらはご自身で考えていただかないと。 ですが、 > txtDateStr = txtDateFortmat.parse(String.valueOf(txtDate)); の部分について、txtDateはEditTextではなかったでしたか? String.valueOf()では入力された値は取れないのではないでしょうか。
Yakusugi

2021/05/04 21:55 編集

``` > txtDateStr = txtDateFortmat.parse(String.valueOf(txtDate)); の部分について、txtDateはEditTextではなかったでしたか? String.valueOf()では入力された値は取れないのではないでしょうか。 ``` 確かにご指摘の通り、受け渡しで問題が発生し、 BudgetTrackerDto.javaの13行目でDate型では無くLong型が受け渡されてしまっている趣旨のエラーメッセージが表示されてしまいました。 MainActivity.javaの52行目から59行目が悪さしてるとは推測しているのですが。。。 上記にアップしたソースコードは全体的に更新しました。 また、エラーメッセージが表示されたスクショもアップしました。
dodox86

2021/05/05 01:55

> BudgetTrackerDto.javaの13行目でDate型では無くLong型が受け渡されてしまっている趣旨のエラーメッセージが表示されてしまいました。 いえ、だって、質問者さん自身がBudgetTrackerDtioクラス自体を修正して、Dateクラス型からlong型の引数に変えてしまい、txtDateStr.getTime()のlong値を渡してしまうようにしているのですから、それは当然です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問