android - Using hour in sqlite -
android - Using hour in sqlite -
when i'm creating table in sqlite, can use, example, "day date". if want hour, can utilize "hour time"? in java class, have:
private time hr
and
contentvalues values = new contentvalues(); values.put("hour", hour.getcurrenttimezone());
in datebase, used "hour date". wanna save in datebase. well, here total code (i think might help):
private databasehelper helper; private int hour, minute; private button hourbutton; private date hour; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.encontro); actionbar actionbar = getsupportactionbar(); actionbar.setdisplayhomeasupenabled(true); calendar calendar = calendar.getinstance() hr = calendar.get(calendar.hour_of_day); minutw = calendar.get(calendar.minute); hourbutton = (button) findviewbyid(r.id.hour); horariocombinadobutton.settext(hour + ":" + minute); helper = new databasehelper(this); } public void selecthour(view view) { showdialog(view.getid()); } @override protected dialog oncreatedialog(int id) { switch (id) { case r.id.hour: homecoming new timepickerdialog(this, hourlistener, hour, minute, true); } homecoming null; } private ontimesetlistener hourlistener = new ontimesetlistener() { public void ontimeset(timepicker view, int hourselected, int minuteselected) { hr = hourselected; min = minuteselected; hourbutton.settext(hour + ":" + minute); } }; public void salvehour(view view) { sqlitedatabase db = helper.getwritabledatabase(); contentvalues values = new contentvalues(); values.put("hour", hour.gettime()); long result = db.insert("selito", null, values); if (result != -1) { toast.maketext(this, getstring(r.string.save), toast.length_short).show(); } else { toast.maketext(this, getstring(r.string.error), toast.length_short).show(); } } @override protected void ondestroy() { helper.close(); super.ondestroy(); }
the date , time functions utilize subset of is0-8601 date , time formats.
the time() function returns time hh:mm:ss
the date() function returns date in format: yyyy-mm-dd
the datetime() function returns "yyyy-mm-dd hh:mm:ss"
based on that:
simpledateformat sdf = new simpledateformat("hh:mm:ss"); date date = new date(hour.gettime()); string databasevalue = sdf.format(date); contentvalues values = new contentvalues(); values.put("hour", databasevalue);
android database sqlite
Comments
Post a Comment