2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mBaasから日付を条件にデータを検索する

Last updated at Posted at 2014-12-01

日付での検索にやたら手間取ったので以下にメモ。
然し本当にgroup_byとかできないんだろうかmBaas……(´@ω@`)

mBaasとは:@nifty mobile backend
想定する動作:今週アップデートされたデータを取得する

今週日曜日(週初め)の日付を取得する

week.java
	DateTime now=DateTime.now();
	DateTime sunday=now.minusDays(now.getDayOfWeek());

	// ちなみに週末土曜日はsundayに7日足せば出てくる
	// DateTime saturday=sunday.plusDays(7);

日付型として処理するため連想配列形式に変換

week.java
	sdf=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
	
	NCMBQuery<NCMBObject> query = NCMBQuery.getQuery("data");
	
	String updateDate=sdf.format(sunday.toDate());
	
	// 連想配列の作成(JSONへの変換はNCMBQuery内で行われている気がする)
	HashMap<String,String> sundayDate=new HashMap<String, String>();
	sundayDate.put("__type", "Date");
	sundayDate.put("iso", updateDate);

	query.whereGreaterThanOrEqualTo("updateDate", sundayDate );

updateDateはmBaasでクラスを作成すると最初からいるやつで、データの更新の際何もしなくてもtimestampみたいに持っていてくれるカラムです。
形式がyyyy-MM-ddThh:flag_mm:ss.SSSZとたいへんややこしいうえ、ただこの形でwhereGraterThanOrEqualToしても引っかかってくれず、条件外してデバッガ回してデータ見比べてようやくこの形に。

もっとスマートな書き方(というか正しい書式)があるのかもしれませんが、取り敢えずこれでヽ(´@ω@`)ノ

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?