1

I simply need to print the date of the day on the console in this format: Day.Month.Year. Example: 03.10.09

Code:

GregorianCalendar c = new GregorianCalendar();
Date s =  c.getTime(); 
System.out.println(s);

The console prints:

Sat Oct 03 13:33:36 CEST 2009

I could do it with a case statement, but I am sure there is something more elegant, which I simply didn't find.

2 Answers 2

5
GregorianCalendar c = new GregorianCalendar();
Date s =  c.getTime();
String dateString = new SimpleDateFormat("dd.MM.yy").format(s);
System.out.println(dateString);
Sign up to request clarification or add additional context in comments.

Comments

3

Use DateFormat and SimpleDateFormat in the java.text package.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class FormatDemo
{

   public static void main(String[] args)
   {
      Calendar c = new GregorianCalendar();
      Date s =  c.getTime();
      DateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd");
      System.out.println(formatter.format(s));
   }
}

1 Comment

gee.. now i found torsten-horn.de/techdocs/java-date.htm this post. SimpleDateFormat is exactly what i need, thx for help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.