Example usage for java.util Calendar getInstance

List of usage examples for java.util Calendar getInstance

Introduction

In this page you can find the example usage for java.util Calendar getInstance.

Prototype

public static Calendar getInstance() 

Source Link

Document

Gets a calendar using the default time zone and locale.

Usage

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("JSpinner Dates");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();/*from www .ja v  a 2s  .  co  m*/
    cal.add(Calendar.YEAR, -50);
    Date startDate = cal.getTime();
    cal.add(Calendar.YEAR, 100);
    Date endDate = cal.getTime();
    SpinnerModel model2 = new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);
    JSpinner spinner2 = new JSpinner(model2);
    JLabel label2 = new JLabel("Range");
    JPanel panel2 = new JPanel(new BorderLayout());

    panel2.add(label2, BorderLayout.WEST);
    panel2.add(spinner2, BorderLayout.CENTER);
    frame.add(panel2, BorderLayout.SOUTH);

    frame.setSize(200, 90);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyy");
    Date date1 = sdf.parse("Thu Oct 03 07:47:22 2015");
    Date date2 = sdf.parse("Mon Jul 05 08:47:22 2015");

    System.out.println(sdf.format(date1));
    System.out.println(sdf.format(date2));

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);//from  w ww .j a  v a  2  s  . c o  m
    cal2.setTime(date2);

    if (cal1.after(cal2)) {
        System.out.println("Date1 is after Date2");
    }

    if (cal1.before(cal2)) {
        System.out.println("Date1 is before Date2");
    }

    if (cal1.equals(cal2)) {
        System.out.println("Date1 is equal Date2");
    }
}

From source file:Main.java

public static void main(String[] args) {
    Calendar someTime = Calendar.getInstance();
    someTime.set(Calendar.HOUR_OF_DAY, 17);
    someTime.set(Calendar.MINUTE, 0);
    someTime.set(Calendar.SECOND, 0);

    // convert it to java epoch date
    Date someDate = toEpochDate(someTime);

    // create a date object from java.sql.Time
    Date fromSqlTime = new Date(Time.valueOf("17:00:00").getTime());

    // now do the comparison
    System.out.println("Some Date: " + someDate.toString());
    System.out.println("Sql Time: " + fromSqlTime.toString());
    System.out.println("Are they equal? " + someDate.equals(fromSqlTime));
}

From source file:Main.java

public static void main(String[] args) {
    String input = "2014-05-04 09:10:40.321";

    // Prepare the pattern
    String pattern = "yyyy-MM-dd HH:mm:ss.SSS";

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    // Parse the text into a Date object
    Date dt = sdf.parse(input, new ParsePosition(0));
    System.out.println(dt);/*from w ww  . j  av a  2 s .c om*/

    // Get the Calendar instance
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);

    // Print time parts
    System.out.println("Hour:" + cal.get(Calendar.HOUR));
    System.out.println("Minute:" + cal.get(Calendar.MINUTE));
    System.out.println("Second:" + cal.get(Calendar.SECOND));
    System.out.println("Millisecond:" + cal.get(Calendar.MILLISECOND));

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));

    out.writeObject(Calendar.getInstance());
    out.writeObject(new BigDecimal("123.123"));
    out.writeInt(1);//w  ww .ja  va2s.c o  m
    out.writeUTF("tutorial");
    out.close();

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    BigDecimal price;
    int unit;
    String desc;

    Calendar date = (Calendar) in.readObject();
    System.out.println(date);

    price = (BigDecimal) in.readObject();
    unit = in.readInt();
    desc = in.readUTF();
    System.out.println(unit);
    System.out.println(desc);
    System.out.println(price);
    in.close();
}

From source file:EventObject.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JButton ok = new JButton("Ok");

    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(event.getWhen());
            Locale locale = Locale.getDefault();
            String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(new Date());

            if (event.getID() == ActionEvent.ACTION_PERFORMED)
                System.out.println(" Event Id: ACTION_PERFORMED");

            System.out.println(" Time: " + s);

            String source = event.getSource().getClass().getName();
            System.out.println(" Source: " + source);

            int mod = event.getModifiers();
            if ((mod & ActionEvent.ALT_MASK) > 0)
                System.out.println("Alt ");

            if ((mod & ActionEvent.SHIFT_MASK) > 0)
                System.out.println("Shift ");

            if ((mod & ActionEvent.META_MASK) > 0)
                System.out.println("Meta ");

            if ((mod & ActionEvent.CTRL_MASK) > 0)
                System.out.println("Ctrl ");

        }// w  w w.j  a  va  2 s .c  om
    });

    f.add(ok);

    f.setSize(420, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);

    BasicFileAttributes attributes = view.readAttributes();
    FileTime lastModifedTime = attributes.lastModifiedTime();
    FileTime createTime = attributes.creationTime();

    long currentTime = Calendar.getInstance().getTimeInMillis();
    FileTime lastAccessTime = FileTime.fromMillis(currentTime);

    view.setTimes(lastModifedTime, lastAccessTime, createTime);
    System.out.println(attributes.lastAccessTime());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("name.txt");

    if (!f.exists()) {
        System.out.println("File not found.");
        return;/*w w  w .  j av a 2  s .c o m*/
    }
    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    long t = Calendar.getInstance().getTimeInMillis();
    if (!f.setLastModified(t))
        System.out.println("Can't set time.");

    if (!f.setReadOnly())
        System.out.println("Can't set to read-only.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    if (!f.setWritable(true, false))
        System.out.println("Can't return to read/write.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    X509CertSelector selec = new X509CertSelector();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in = new FileInputStream(args[0]);
    Certificate c = cf.generateCertificate(in);
    System.out.println(selec.match(c));
    selec.setIssuer("CN=Peter,OU=Network Center," + "O=University,L=ZB,ST=Vancouver,C=CN");

    System.out.println(selec.match(c));

    Calendar cld = Calendar.getInstance();
    int year = Integer.parseInt(args[1]);
    int month = Integer.parseInt(args[2]) - 1;
    int day = Integer.parseInt(args[3]);
    cld.set(year, month, day);/*  w w  w .java  2s.com*/
    Date d = cld.getTime();
    selec.setCertificateValid(d);

    System.out.println(selec.match(c));
    BigInteger sn = new BigInteger("1039056963");
    selec.setSerialNumber(sn);

    System.out.println(selec.match(c));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE );");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
    pstmt.setDate(2, sqlDate);/*from   w  w  w . j  a v a 2 s . c o  m*/

    pstmt.executeUpdate();

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    Calendar cal = Calendar.getInstance();

    // get the TimeZone for "America/Los_Angeles"
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    cal.setTimeZone(tz);

    while (rs.next()) {
        // the JDBC driver will use the time zone information in
        // Calendar to calculate the date, with the result that
        // the variable dateCreated contains a java.sql.Date object
        // that is accurate for "America/Los_Angeles".
        java.sql.Date dateCreated = rs.getDate(2, cal);
        System.out.println(dateCreated);
    }

    rs.close();
    st.close();
    conn.close();
}