Hi, I'm just learning to program and I'm stuck on a basic problem, printing today's date in a form typified by (e.g.) 28 February 2011

import java.util.*; 

	class Date 
	{
 	   	int day, month, year;
	}

	class PrintToday 
	{
		static Date thisDay() 
		{
			Date today = new Date();
			GregorianCalendar c = new GregorianCalendar();	
			today.day = c.get(Calendar.DATE);
			today.month  = c.get(Calendar.MONTH)+1;
			today.year  = c.get(Calendar.YEAR);
			return today;
		}

	
		static String monthName(int m) 
		{
			if (m==1) return "January";
			else if (m==2) return "February";
			else if (m==3) return "March";
			else if (m==4) return "April";
			else if (m==5) return "May";
			else if (m==6) return "June";
			else if (m==7) return "July";
			else if (m==8) return "August";
			else if (m==9) return "September";
			else if (m==10) return "October";
			else if (m==11) return "November";
			else return "December";
		}
		
    		public static void main(String[] args) 
		{		
		       .......
		}
	}

I'm not sure how to implement these classes/methods in the main method.

Dani AI

Generated

The simplest, safest way to print "28 February 2011" is to use the modern java.time API (Java 8+). Two practical notes first: the original post defines a class named Date which will shadow java.util.Date — rename that class (for example to MyDate) — and avoid a hand-rolled month-name chain because Calendar.MONTH is zero-based and off-by-one errors are common.

A compact Java 8+ solution:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class ShowToday {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("d MMMM uuuu", Locale.ENGLISH);
        System.out.println(today.format(fmt)); // e.g. "28 February 2011"
    }
}

If you must target pre-Java-8 environments, use SimpleDateFormat (but beware it is not thread-safe):

import java.text.SimpleDateFormat;
import java.util.Locale;

public class ShowTodayLegacy {
    public static void main(String[] args) {
        SimpleDateFormat fmt = new SimpleDateFormat("d MMMM yyyy", Locale.ENGLISH);
        System.out.println(fmt.format(new java.util.Date()));
    }
}

Extra tips: use Locale to control the language of month names; use ZonedDateTime/ZoneId if you care about server vs. local timezone; avoid manual month-name mappings and if/else chains; and prefer DateTimeFormatter over SimpleDateFormat for safety and clarity. As noted this belongs in the Java area and moved it; pointed to example collections — the links below show the official formatter and date classes for reference.

LocalDate docs: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
DateTimeFormatter docs: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
SimpleDateFormat docs: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Calendar note (zero-based months): https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html

Recommended Answers

All 3 Replies

Agreed. Moving.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.