Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/** ***************************************************************************
 * DATE EXAMPLE CLASS
 * @author stanislav.zorjan
 */
public class DateExample {
    
    
     //
     // INSTEAD OF USING STANDARD DATE OR CALENDAR
     // USE "JODA" LIBRARY FOR DEALING WITH DATE
     // http://joda-time.sourceforge.net/
     //
    
    private Date date = new Date();
    private DateFormat format;
    
    
    
    /** ***********************************************************************
     * Default constructor
     */
    public DateExample() {
	try {
	    createDate();
	} catch (ParseException ex) {
	    Logger.getLogger(DateExample.class.getName()).log(Level.SEVERE, null, ex);
	}
    }
    
    /** ************************************************************************
     * Creating date
     * @throws ParseException 
     */
    private void createDate() throws ParseException{
	// creates date with current date/time
	date = new Date();
	System.out.println(date.toString());
	
	// creating date with current date/time
	date = Calendar.getInstance().getTime();
	System.out.println(date.toString());
	
	// setting callendar to specified date
	format = new SimpleDateFormat();
	format.getCalendar().set(1978, 6, 8, 0, 0, 0);
	System.out.println(format.getCalendar().getTime());
	
	// creating specified date by parsing date string 
	format = new SimpleDateFormat("dd.MM.yyyy", Locale.ITALY);
	date = format.parse("08.07.1978");
	System.out.println(date);
	
	// creating specified date by parsing date string
	format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
	date = format.parse("2001.07.04 AD at 12:08:56 PDT");
	System.out.println(date);
	
	System.out.println("");
	
	format = DateFormat.getDateInstance(DateFormat.FULL, Locale.GERMAN);
	System.out.println(format.format(date));
	
	format = DateFormat.getDateInstance(DateFormat.LONG, Locale.GERMAN);
	System.out.println(format.format(date));
	
	format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN);
	System.out.println(format.format(date));
	
	format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
	System.out.println(format.format(date));
	
	System.out.println("");
	
	// outputing formated date
	format = new SimpleDateFormat("dd.MM.yy");
	System.out.println(format.format(date));
	
	// outputing formated date
	format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z", Locale.GERMAN);
	System.out.println(format.format(date));
	
	
	System.out.println("");
	
	
	// formating to current date/time
	format.format(new Date());
	
	// return year
	System.out.println(format.getCalendar().get(Calendar.YEAR));
	
	// return Month
	System.out.println(format.getCalendar().get(Calendar.MONTH));
	
	// return Day of the Month
	System.out.println(format.getCalendar().get(Calendar.DATE));
	
	// return Day of the Week
	System.out.println(format.getCalendar().get(Calendar.DAY_OF_WEEK)-1);
	
	// return Day of Year
	System.out.println(format.getCalendar().get(Calendar.DAY_OF_YEAR));
	
	// return Day of the Week in the Month
	System.out.println(format.getCalendar().get(Calendar.DAY_OF_WEEK_IN_MONTH));
	
	// return Hour
	System.out.println(format.getCalendar().get(Calendar.HOUR_OF_DAY));
	
	// return Minutes
	System.out.println(format.getCalendar().get(Calendar.MINUTE));
	
	// return Seconds
	System.out.println(format.getCalendar().get(Calendar.SECOND));
	
	// return Milliseconds
	System.out.println(format.getCalendar().get(Calendar.MILLISECOND));
	
	// return Time in Milliseconds
	System.out.println(date.getTime());
	
	
    }
    
}