我的Android代码存在问题。我只想从json那里得到日期。 然后我想拿这个日期和他们一起我想制作一个AlarmManager。 但问题是,变量“月份日”是一些不同于日期的整数值。 日期格式是这样的:20.04.2014
预期:2014年4月20日但输出:天= 0月= 2年= 114
有人在我的代码中看到了错误的东西吗? 谢谢:))
我的Longclick听众:
listv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
Object o = listv.getItemAtPosition(pos);
JsonStrings obj= (JsonStrings)o;
Date date=obj.ConvertToDate(obj.begin);
Calendar calendar = Calendar.getInstance();
int month = date.getMonth()-1;
int year=date.getYear();
int day=date.getDay();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
return true;
}
});
我的JsonStrings.class:
public class JsonStrings {
public String name;
public String kategorie;
public String begin;
public String end;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKategorie() {
return kategorie;
}
public void setKategorie(String kategorie) {
this.kategorie = kategorie;
}
public String getBegin() {
return begin;
}
public void setBegin(String begin) {
this.begin = begin;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public Date ConvertToDate(String date) {
String str_date = date;
DateFormat formatter;
formatter = new SimpleDateFormat("dd.MM.yyyy");
Date newdate = null;
try {
newdate = (Date) formatter.parse(str_date);
} catch (ParseException e) {
e.printStackTrace();
}
return newdate;
}