问题
i'm trying to get the current date and time formatted, this is my code:
Date date = new Date();
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd-HHmm");
String formattedDate=""+FORMATTER.format(date);
Log.d("XXxxxXxXXXXxxxXXXxXxxxX",formattedDate);
Something is going wrong because i'm getting this string: 19700103-0217
That date is incorrect, I should get something like this: 20111118-1217
How to solve this problem?
回答1:
Well, I can help you out but I have my own code. Ask me if you don't get something.
First of all you have to make a class of any name like date.class and write the code:
public class Date{
private Date dt;
private String stDt;
private String enDt;
SRSDDate(){
dt=new Date();
int mn;
int yr;
mn=dt.getMonth();
yr=dt.getYear();
mn=mn+1;
yr=1900+yr;
stDt="1/"+mn+"/"+yr;
switch(yr%4){
case 0:
if(mn==2){
enDt="29/"+mn+"/"+yr;
}
else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10||mn==12){
enDt="31/"+mn+"/"+yr;
}
else{
enDt="30/"+mn+"/"+yr;
}
break;
default:
if(mn==2){
enDt="28/"+mn+"/"+yr;
}
else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10||mn==12){
enDt="31/"+mn+"/"+yr;
}
else{
enDt="30/"+mn+"/"+yr;
}
break;
}
}
public String getTodayDate(){
return (dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate();
}
public String getTodayDisplayDate(){
return dt.getDate()+"/"+(dt.getMonth()+1)+"/"+(dt.getYear()+1900);
}
public String getStDt(){
return stDt;
}
public String getEnDt(){
return enDt;
}
}
After making the class, you have to declare this:
Calendar FromDateCal=Calendar.getInstance();
DatePickerDialog.OnDateSetListener fd=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
FromDateCal.set(Calendar.YEAR, year);
FromDateCal.set(Calendar.MONTH, monthOfYear);
FromDateCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateFromDate();
}
};
Now find the edit box in which you want to display the date like:
Date dt=new Date();
FromDateText=(EditText)findViewById(R.id.FromDateText);
FromDateText.setText(dt.getStDt());
Now you have to display date on edit box as follows:
private void updateFromDate(){
int iDay;
int iMonth;
int iYear;
iDay=FromDateCal.get(Calendar.DATE);
iMonth=FromDateCal.get(Calendar.MONTH);
iMonth=iMonth+1;
iYear=FromDateCal.get(Calendar.YEAR);
String sFDate=iDay+"/"+iMonth+"/"+iYear;
FromDateText.setText(sFDate);
}
I hope you got my answer.
回答2:
The problem is that your date is incorrect. It's hard to say how, as you haven't shown where it's coming from, but that's pretty much bound to be the problem. To confirm this, use the built-in Date.toString()
method - that uses the system default time zone, so you need to be careful, but you should at least see the right date etc.
Log.d("date.toString() - ", date.toString());
回答3:
I would say the device (or emulator) on which you execute your code has an incorrect date. Your code should work as expected.
In any case, it's not related to SimpleDateFormat
, ythe way you use it is correct.
回答4:
If your goal is just to show current date then you can use Calendar
though.
Using calendar,its pretty easy to get the date in desired format.
Ex:
Calendar cal=Calendar.getInstance();
String year=String.format("%1$tY",cal);
String month=String.format("%1$tm",cal);
String day=String.format("%1$td",cal);
String hour=String.format("%1$tH",cal); // hour in 24 hour time format
String min=String.format("%1$M",cal);
String finalDateString=year+month+day+"-"+hour+min; //gives you in format like 20111118-1217
For more formates,refer this.
回答5:
To get Current Date. Try this
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
回答6:
Calendar calendar=Calendar.getInstance();
//This will get you the current month.
Date date = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("MMM");
String currentMonth = format.format(date);
//This will get you the current date of the month.
SimpleDateFormat format1 = new SimpleDateFormat("dd"); String currentDate = format1.format(date);
If you need other formats, you can see this link: http://developer.android.com/reference/java/text/SimpleDateFormat.html
来源:https://stackoverflow.com/questions/8181521/how-to-use-simpledateformat-to-show-the-current-date