How to use SimpleDateFormat to display current date?

I am 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 goes wrong because I get this line: 19700103-0217

This date is incorrect, I should get something like this: 20111118-1217

How to solve this problem?

0
source share
6 answers

Ok, I can help you, but I have my own code. Ask me if you get something.

First of all, you must create a class of any name, for example 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 creating the class, you should 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, for example:

 Date dt=new Date(); FromDateText=(EditText)findViewById(R.id.FromDateText); FromDateText.setText(dt.getStDt()); 

Now you should display the date in the 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); } 

Hope you got my answer.

-1
source

The problem is that your date is incorrect. It is difficult to say how, as you have not shown, where it comes from, but it is largely related to the problem. To confirm this, use the built-in Date.toString() method, which uses the default system time zone, so you need to be careful, but you should at least see the correct date, etc.

 Log.d("date.toString() - ", date.toString()); 
+2
source

I would say that the device (or emulator) on which you are executing your code has the wrong date. Your code should work as expected.

In any case, this is not related to SimpleDateFormat , since you are using it correctly.

+1
source

If your goal is to show the current date, you can use Calendar though.

Using a calendar, it’s pretty easy to get the date in the right format.

Example:

 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 

See this for more information.

+1
source

Get the current date. try it

  Calendar c = Calendar.getInstance(); System.out.println("Current time => " + c.getTime()); SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); String formattedDate = df.format(c.getTime()); 
+1
source
 Calendar calendar=Calendar.getInstance(); 

// This will give you the current month.

Date date = calendar.getTime (); SimpleDateFormat format = new SimpleDateFormat ("MMM");

  String currentMonth = format.format(date); 

// This will give 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

0
source

Source: https://habr.com/ru/post/1381889/


All Articles