Shervin Asgari wrote:
> Hi.
>
> I am a first time user of Joda-Time. I need to output a date in a
> certain format and also add one day to the current day.
>
> I have this code:
> DateTime dt = new DateTime();
> System.out.println(dt.getYear() + " " + dt.getMonthOfYear() + " " +
> dt.getDayOfMonth()+1);
>
> The problem is that I need this output to be in a certain format. Like
> yyyy-MM-dd and MM outputs 6 and not 06.
> How can I make this happen?
>
> I tried
> DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
> but System.out.println(fmt); only writes the memory allocation :-/
>
> I also tried DateTimeFormatter fmt =
> DateTimeFormat.forPattern("yyyy-MM-dd"); with the same result.
>
> Any suggestions?
>
This might do what you want...
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateTimeFormatTest {
public static void main(String[] args) {
DateTime dt = new DateTime();
// Print months with leading 0 if needed...
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
System.out.println(format.print(dt));
// years, months, weeks, days, hours, mins,
secs, millis
Period day = new Period( 0, 0, 0, 1, 0,
0, 0, 0);
System.out.println(format.print(dt.plus(day)));
}
}
Use: DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-M-dd");
if you want months without leading zeros.
Roland
> Shervin
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Joda-interest mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
--
The contents of this message are mine personally and do not necessarily reflect
any position of the Government or the National Oceanic and Atmospheric
Administration.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest