Convert date from "2009-12 Dec" to the format "31-DEC-2009"

'2009-12 Dec' should be converted to '31-DEC-2009' '2010-09 Sep' should be converted to '30-SEP-2010' '2010-02 Feb' should be converted to '28-FEB-2010' '2008-02 Feb' should be converted to '29-FEB-2008' 

Values 2009-12 December , 2008-02 Feb will be displayed to the user in the drop-down list. The user is not able to select DAY .

The value selected by the user must be transferred to the database. But the database expects a date in the format DD-MMM-YYYY . The query has the condition " <= USER_DATE ". Thus, the last day of the month should be automatically selected and transferred to the database.

Pl help me write a function that does the above job.

 static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM MMM"); public static String convertMapedToSqlFormat(final String maped) { String convertedMaped = null; //.... return convertedMaped; } @Test public void testConvertMapedToSqlFormat() { String[] mapedValues = { "2009-12 Dec", "2009-11 Nov", "2009-10 Oct", "2009-09 Sep", "2009-08 Aug", "2009-07 Jul", "2009-06 Jun", "2009-05 May", "2009-04 Apr", "2009-03 Mar", "2009-02 Feb", "2009-01 Jan", "2008-12 Dec", "2008-11 Nov", "2008-10 Oct" }; for (String maped : mapedValues) { System.out.println(convertMapedToSqlFormat(maped)); } } 
+4
source share
5 answers

Convert it to Calendar and use Calendar#getActualMaximum() to get the last day of the month and set the day with it.

Kickoff example:

 String oldString = "2009-12 Dec"; Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(oldString)); // Yes, month name is ignored but we don't need this. calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); String newString = new SimpleDateFormat("dd-MMM-yyyy").format(calendar.getTime()).toUpperCase(); System.out.println(newString); // 31-DEC-2009 
+7
source
  • Use DateFormat (but correct it before yyyy-dd MMM ) to parse the date
  • convert Date to Calendar
  • Use Calendar.getActualMaximim()
  • use dd-MMM-yyyy to format the received date.
  • call .toUpperCase()

So:

 static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM MMM"); static SimpleDateFormat dbDateFormat = new SimpleDateFormat("yyyy-MMM-dd"); public static String convertMapedToSqlFormat(final String maped) { Date date = dateFormat.parse(mapped); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return dbDateFormat.format(cal.getTime()).toUpperCase(); } 

A few notes:

  • if possible use joda-time DateTime
  • Avoid having strict date formats in the database.
+2
source

Get the year and month from part of the YYYY-MM row.

Use JODA to create a point in time corresponding to the first day of this month. Move one month forward and one day ago. Reduce the time to the string representation you need.

+1
source

Hi, you need to analyze the date, so

  SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); Date du = new Date(); du = df.parse(sDate); df = new SimpleDateFormat("yyyy-MM-dd"); sDate = df.format(du); 

Hope this helps. Let me know if this happens.

PC

0
source

java.time

It is much easier now with the modern java.time classes that supplant the unpleasant old time and time classes discussed here in Question and other answers.

The java.time framework is built into Java 8 and later. These classes supersede old inconvenient time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .

Now, in Joda-Time maintenance mode, the project also advises switching to java.time.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.

Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP .

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time.

YearMonth

The YearMonth class provides only what you want.

 YearMonth start = YearMonth.of( 2008 , Month.OCTOBER ); YearMonth stop = YearMonth.of( 2009 , Month.DECEMBER ); List<YearMonth> yms = new ArrayList<>(); YearMonth ym = start ; while( ! ym.isAfter( stop ) ) { yms.add( ym ); // Set up the next loop. ym = ym.plusMonths( 1 ); } 

To represent, use DateTimeFormatter to create a string representation of the value of YearMonth .

 DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM MMM" ); f = f.withLocale( Locale.CANADA_FRENCH ); // Or Locale.US etc. String output = ym.format( f ); 

To retrieve the last day of the month, YearMonth object.

 LocalDate endOfMonth = ym.atEndOfMonth(); 

To submit, use a DateTimeFormatter . Either let it create an instance of formatting that is automatically localized according to the specified Locale or specify your own formatting template. Shown many times in many other stack overflow questions and answers.

0
source

All Articles