lukaaa
lukaaa

Reputation: 25

android string datetime format with month and day

i have like this datetime 09/01/2014 and i want to format this datetime like this 1 september. i wrote some code and i can format this date like this

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

                        Date _d = df.parse("09/01/2014");                   
                        SimpleDateFormat new_df = new SimpleDateFormat(
                                "dd");
                        String _s = new_df.format(_d);
                        cinemaTime.setStartTime(_s);

in this code result is onlu 01 but i don't know how i can recive 1 september(9th month of years( if anyone knows solution please help me thanks

Upvotes: 1

Views: 1100

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338496

tl;dr

MonthDay                          // Represent a month with day-of-month, but without year, time-of-day, time zone, nor offset. 
.of ( Month.SEPTEMBER , 1 )
.format( 
    DateTimeFormatter
    .ofPattern ( "d MMMM" )        // d for day-of-month without padding zero. MMMM for month name in full.
    .withLocale ( 
        Locale.of ( "fr" , "CA" )  // French language, Canada cultural norms.
    )
)

java.time

In modern Java, use the java.time classes.

Android 26+ bundles an implementation of java.time. For earlier Android, the latest tooling provides must of the java.time functionality via “API desugaring”.

LocalDate

You said:

i have like this datetime 09/01/2014

For a date-only value, without time-of-day, and without time zone or offset, use java.time.LocalDate.

LocalDate ld = LocalDate.of( 2014 , Month.SEPTEMBER , 1 ) ;

MonthDay

format this datetime like this 1 September

You want only the day and the month, so extract an object to represent that. Use MonthDay class.

MonthDay md = MonthDay.from( ld ) ;

Or just start with a MonthDay.

MonthDay md = MonthDay.of( Month.SEPTEMBER , 1 ) ;

ISO 8601

Generate text in standard ISO 8601 format.

String output = md.toString() ;

Localization

To generate text in a localized format, define a formatting pattern. Specify a Locale to determine the human language and cultural norms to be used in localization.

Locale locale = Locale.of( "en" , "US" ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d MMMM" ).withLocale( locale ) ;
String output = md.format( f ) ;

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126455

this method will do the job if you want an output like "1 september 2014".

public static String dateFormatterforLukka(){       
    String inputDate = "09/01/2014";        
    String inputFormat = "MM/dd/yyyy";
    String outputFormat = "d ' ' MMMM ' ' yyyy";

    Date parsed = null;
    String outputDate = "";
    try {
        SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("en", "US"));
        SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("en", "US"));                    
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);
    } catch (Exception e) { 
        outputDate = inputDate;     
    }
    return outputDate;
}

Read the documentation about: SimpleDateFormat Class

Upvotes: 3

ipavl
ipavl

Reputation: 828

You're only getting "01" because that's all you asked it to give you ("dd"). MMMM will give you the month as a word. If you want just "1" instead of "01", you would just use d instead of dd.

Further documentation: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 0

Related Questions