So doing my first year of software engineering at Uni and am having a little trouble with the old Java,

Basically I need to use the Calendar class to call todays date, and then store it in a variable, Iv stumbled upon this bit of code but don’t really understand what’s going on enough to be able to re-write it to do what i want it to do, If someone could help me out without giving the answer (don’t learn that way!) And maybe point me in the right direction would be much appreciated,

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    
    public void convertDateToString() {
        
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 
        try {
            
            System.out.println("Today: " + dateFormat.format(calendar.getTime()));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    public static void main(String[] args) {
        new Main().convertDateToString();
    }

}

As it stand this prints todays date in a console window, But I don't want to use it, I want to fully understand what its doing then build something up myself,

Hope someone is able to help me out!

Cheers

Simply creating a Calendar gets you one with todays date, and as long as that "creation" is taking place on the right hand side of an assignment expression, it has also been stored in a variable.

Calendar calendar = Calendar.getInstance();

As masijade said, this is creating a Calendar instance, and its default is the current date/time.. however, the Calendar object doesn't have an empty constructor like .. JPanel for example

The way to create a Calendar object is to call the STATIC method getInstance() on the Calendar class, this returns back your very own Calendar object you can mess about with

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

This is creating a SimpleDateFormat class, this class is used for parsing java.util.Date into Strings and vica versa .. here, it is using "dd/MM/yyyy"

Check this page for the options, you can show times, AM/PM/24hr, months as numbers, names etc - http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

System.out.println("Today: " + dateFormat.format(calendar.getTime()));

and Finally, this System.out.println is printing out the java.util.Date it gets from your calendar object 'getTime()' and makes a string formated as 'dd/MM/yyyy'

On your calendar object, you can also do things like 'calender.get(Calendar.HOUR_OF_DAY)' or 'calendar.add(Calendar.HOUR_OF_DAY, -2)' or 'calendar.set(Calendar.HOUR_OF_DAY, 4)'

(works with all the other options, Calendar.MONTH, Calendar.SECOND etc etc)

Its a lovely class really :)

I understand most of that, Just not sure where the actually variable is being stored, If i was to add and minus information off the current date, What is the variable I would be using? Or can i simple create a new one with something like

currentDate = System.out.println("Today: " + dateFormat.format(calendar.getTime()));

And then be using currentDate to manipulate the data?

That piece of code won't compile given that the println method of PrintStream returns void.

Look into some examples which use the Calendar API for adding/subtracting dates/months/years.

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.