Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : date format in a String

Tags:

I have a problem to sort date because of the format of these dates.

I obtain the date :

final Calendar c = Calendar.getInstance();     mYear = c.get(Calendar.YEAR);     mMonth = c.get(Calendar.MONTH);     mDay = c.get(Calendar.DAY_OF_MONTH); 

And I build a String with these values.

dateRappDB = (new StringBuilder()    .append(mYear).append(".")    .append(mMonth + 1).append(".")    .append(mDay).append(" ")).toString(); 

The problem is that if the month is for example February, mMonth value is 2. So dates with months like October (10) comes before in my list.

What I need is that month and day are formated like MM and dd. But I don't know how to do it in my case.

EDIT :

I solved the problem by using a DateFormat like said above.

I replaced this :

dateRappDB = (new StringBuilder()   .append(mYear).append(".")   .append(mMonth + 1).append(".")   .append(mDay).append(" ")).toString(); 

By this :

Date date = new Date(mYear - 1900, mMonth, mDay); dateFacDB = DateFormat.format("yyyy.MM.dd", date).toString(); 

And it works. Thanks to all of you for your help :)

like image 205
HerrM Avatar asked Sep 02 '11 09:09

HerrM


People also ask

How can I change the date format in Android?

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a"); String date = formatter. format(Date. parse("Your date string")); Hope it will help you.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How do I convert a date to a specific format?

Convert date to different format with Format CellsSelect the dates you want to convert, right click to select Format Cells from context menu. 2. In the Format Cells dialog, under Number tab, select Date from Category list, and then select one format you want to convert to from the right section.

How do I format a simple date?

For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by Character.


2 Answers

here is a simple way to convert Date to String :

SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");

String strDt = simpleDate.format(dt);

like image 103
Vinayak Avatar answered Oct 16 '22 11:10

Vinayak


 Calendar calendar = Calendar.getInstance();  Date now = calendar.getTime();     String timestamp = simpleDateFormat.format(now); 

These might come in handy

SimpleDateFormat simpleDateFormat =     new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"); 

this format is equal to --> "2016-01-01T09:30:00.000000+01:00"

SimpleDateFormat simpleDateFormat =     new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ"); 

this format is equal to --> "2016-06-01T09:30:00+01:00"

like image 43
THZ Avatar answered Oct 16 '22 11:10

THZ