2011-11-23 3 views
0

저는 새로운 스타터 Android입니다. 간단한 질문이 있습니다. datePickerDialog를 통해 날짜를 선택하고 확인 버튼을 누르면 문자열 대신 날짜 형식으로 날짜를 저장하려고합니다. 날짜 형식을 어떻게 저장할 수 있습니까?안드로이드 데이터베이스 날짜 유형

private static final String DATABASE_CREATE = 
"create table fridge_table (_id integer primary key autoincrement, "+ 
"category text not null, name texct not null, expired_date text not null);"; 

public void onCreate(SQLiteDatabase db) 
{ 
    db.execSQL(DATABASE_CREATE); 
} 

어떻게 만료 된 날짜 필드를 변경할 수 있습니까? 일부 사람들은 길거나 날짜를 사용했다. 나는 sqlite에 저장하는 방법을 궁금해.

날짜를 변환하려고하는데이 필드를 통해 알람을 설정하고 싶습니다. 도와 주시겠습니까? 어떤 예가 있으면 몇 가지 예를 들어주세요.

답변

2

다음 일정 개체를 사용할 수 있습니다.

당신은 같은 시간을 절약 할 수 있습니다 : 당신은 또한 사용하여 동일한를 검색 할 수 있습니다

Calendar cal=Calendar.getInstance(); 
//set date,month,year,time etc. according to your need to cal object 
//add expired_date's value as cal.getTimeInMillis() 

을 : 다양한 날짜 형식 옵션을 보려면

long date=cursor.getLong(cursor.getColumnIndex("expired_date")); 
Calendar cal=Calendar.getInstance(); 
cal.clear(); 
cal.setTimeInMillis(date); 
// you can use any format to display this time using String.format() method. 

방문 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

편집 :

예를 들어 충분 :

Calendar cal=Calendar.getInstance(); 
String date_time=String.format("%1$te %1$tB %1$tY,%1$tI:%1$tM:%1$tS %1$Tp",cal); 
Toast.makeText(getApplicationContext(),date_time,Toast.LENGTH_SHORT).show(); 
//this will show a toast containing current date and time in format "23 January 2010,12:30:15 pm" 
+0

그렇다면 String 유형을 Long 유형으로 먼저 변경해야합니까? – wholee1

+0

실제로는 ... 당신은 단지 시간/날짜를 long으로 바꾸기 만하면됩니다. 내 답변에 제공된 링크에서 formates를 사용하여 사람이 읽을 수있는 문자열로 변환 할 수 있습니다. 내 편집 된 답변을 참조하십시오. 나는 거기에 예제를 제공하고 있습니다! – Hiral

+0

답장을 보내 주셔서 감사합니다. 귀하의 코딩 작업을하려고하지만 여전히 작동하지 않습니다. 나는 노력하고있어. String date_format = String.format ("% td- % tm- % tY", cal); 시간 대신에. 맞아? 원본 텍스트는 '24/11/2011 '과 같았습니다. 올바른 형식인가요? '-'를 '/'로 변경해야합니까? 내가 이걸하려고하면 나는 그것을 얻지 못한다. – wholee1

3

SQLite 버전 3은 다음 데이터 유형 만 지원하므로 날짜 유형으로 날짜를 저장할 수 없습니다. 검색하고 당신은 긴 유형의 형식에서 날짜를 변환 할 수 있습니다

NULL. The value is a NULL value. 
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes 
    depending on the magnitude of the value. 
REAL. The value is a floating point value, stored as an 8-byte IEEE floating 
    point number. 
TEXT. The value is a text string, stored using the database encoding (UTF-8, 
    UTF-16BE or UTF-16LE). 
BLOB. The value is a blob of data, stored exactly as it was input. 
0

데이터베이스에서 저장 할 때 긴 데이터의 밀리 초를 가지게됩니다 당신은 날짜 변환에 텍스트를 수행하는 자바의 달력 클래스를 사용해야합니다 유형. 그런 다음 Long에서 해당 밀리 초를 문자열로 변환하고 데이터베이스의 TEXT 데이터 유형으로 저장할 수 있습니다.