Example usage for java.util Date Date

List of usage examples for java.util Date Date

Introduction

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

Prototype

public Date() 

Source Link

Document

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Usage

From source file:Main.java

/** Typical main method ("main program") declaration */
public static void main(String[] av) {

    Locale l1 = new Locale("en", "US"), l2 = new Locale("es", "ES");

    // Create a Date object for May 5, 1986
    Calendar c = Calendar.getInstance();
    c.set(1986, 04, 05); // May 5, 1986
    Date d1 = c.getTime();/*  ww  w  . j a v a 2  s .c  o m*/

    // Create a Date object for today.
    Date d2 = new Date(); // today

    DateFormat df_us = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l1),
            df_sp = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l2);
    System.out.println("Date d1 for US is " + df_us.format(d1));
    System.out.println("Date d1 for Spain is " + df_sp.format(d1));
    System.out.println("Date d2 is " + df_us.format(d2));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Order o = new Order();
    o.setCustId(123);//from   www  .jav  a 2  s.  c  o  m
    o.setDescription("New order");
    o.setOrderDate(new Date());

    List<Item> items = new ArrayList<Item>();

    Item i = new Item();
    i.setName("PC");
    i.setQty(10);
    items.add(i);

    i = new Item();
    i.setName("Box");
    i.setQty(4);

    items.add(i);

    o.setItems(items);
    // Write it
    JAXBContext ctx = JAXBContext.newInstance(Order.class);

    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    m.marshal(o, sw);
    sw.close();
    System.out.println(sw.toString());

    // Read it back
    JAXBContext readCtx = JAXBContext.newInstance(Order.class);
    Unmarshaller um = readCtx.createUnmarshaller();

    Order newOrder = (Order) um.unmarshal(new StringReader(sw.toString()));
    System.out.println(newOrder);
}

From source file:FillTest.java

public static void main(String args[]) {
    int array[] = new int[10];
    Arrays.fill(array, 100);/*from  ww w  . java2 s.  c  om*/
    for (int i = 0, n = array.length; i < n; i++) {
        System.out.println(array[i]);
    }
    System.out.println();
    Arrays.fill(array, 3, 6, 50);
    for (int i = 0, n = array.length; i < n; i++) {
        System.out.println(array[i]);
    }
    byte array2[] = new byte[10];
    Arrays.fill(array2, (byte) 4);

    System.out.println();
    Date array3[] = new Date[10];
    Date anObject = new Date();
    Arrays.fill(array3, "Help");
    anObject.setYear(102);
    for (int i = 0, n = array3.length; i < n; i++) {
        System.out.println(array3[i]);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    final MaskFormatter formatter = new TimeFormatter();
    formatter.setValueClass(java.util.Date.class);
    final JFormattedTextField tf2 = new JFormattedTextField(formatter);
    tf2.setValue(new Date());
    final JLabel label = new JLabel();
    JButton bt = new JButton("Show Value");
    bt.addActionListener(e -> {//from w  w w.  j a v  a2s  .co m
        System.out.println(" value 2 = " + tf2.getValue());
        System.out.println(" value 2 = " + tf2.getText());
        System.out.println("value class: " + formatter.getValueClass());
        label.setText(tf2.getText());
    });
    JFrame f = new JFrame();
    f.getContentPane().setLayout(new GridLayout());
    f.getContentPane().add(tf2);
    f.getContentPane().add(label);
    f.getContentPane().add(bt);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String startDate = "14-08-2014";
    String endDate = "21/08/2020";

    SimpleDateFormat sdf[] = new SimpleDateFormat[] { new SimpleDateFormat("dd/MM/yyyy"),
            new SimpleDateFormat("dd-MM-yyyy") };

    Date dateStart = parse(startDate, sdf);
    Date dateEnd = parse(endDate, sdf);

    System.out.println("Is today bewteen " + dateStart + " and " + dateEnd);
    Date today = new Date();
    if (today.after(dateStart) && today.before(dateEnd)) {
        System.out.println("Yes");
    } else {/*w  w  w .  ja  v a 2 s .  com*/
        System.out.println("No");
    }
}

From source file:Main.java

public static void main(String[] args) {
    // Formatting strings
    System.out.printf("%1$s, %2$s,  and  %3$s %n", "ABC", "DEF", "XYZ");
    System.out.printf("%3$s, %2$s,  and  %1$s %n", "ABC", "DEF", "XYZ");

    // Formatting numbers
    System.out.printf("%1$4d, %2$4d, %3$4d %n", 1, 10, 100);
    System.out.printf("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000);
    System.out.printf("%1$-4d, %2$-4d,  %3$-4d %n", 1, 10, 100);
    System.out.printf("%1$-4d, %2$-4d,  %3$-4d %n", 10, 100, 1000);

    // Formatting date and time
    Date dt = new Date();
    System.out.printf("Today is  %tD  %n", dt);
    System.out.printf("Today is  %tF  %n", dt);
    System.out.printf("Today is  %tc  %n", dt);
}

From source file:Main.java

public static final void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.OracleDriver");
    Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password");
    Date nowDate = new Date();
    Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
    PreparedStatement insertStmt = conn.prepareStatement(
            "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)");
    insertStmt.setString(1, System.getProperty("os.name"));
    insertStmt.setTimestamp(2, nowTimestamp);
    insertStmt.setTimestamp(3, nowTimestamp);
    insertStmt.setTimestamp(4, nowTimestamp);
    insertStmt.executeUpdate();//from ww w .  j  ava  2 s  .  c o  m
    insertStmt.close();
    System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");
    PreparedStatement selectStmt = conn
            .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable");
    ResultSet result = null;
    result = selectStmt.executeQuery();
    while (result.next()) {
        System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(),
                result.getTimestamp(3).toString(), result.getTimestamp(4).toString()));
    }
    result.close();
    selectStmt.close();
    conn.close();
}

From source file:x595.Main.java

public static void main(String... args) {
    @SuppressWarnings("resource")
    ApplicationContext ctx = new AnnotationConfigApplicationContext(JpaConfig.class);

    CarRepository repo = ctx.getBean(CarRepository.class);

    log("Start testing CarRepository");

    LicenceInfo info1 = new LicenceInfo("Jane Doe", "jane@corp.com", new Date(), "N17D3E");
    LicenceInfo info2 = new LicenceInfo("Josh Maxwell", "josh@googol.com", new Date(), "G99331");
    LicenceInfo info3 = new LicenceInfo("JD", "saxe@me.com", new Date(), "HL9010");
    LicenceInfo info4 = new LicenceInfo("Triple corp", "triple@doodle.org", new Date(), "JDC13X");
    LicenceInfo info5 = new LicenceInfo("JD", "pro4@k5.com", new Date(), "A75Dc1");

    Car c1 = new Car("AMG", "A4", 2012, 110, info1);
    Car c2 = new Car("Genesis", "G20", 2008, 170, info2);
    Car c3 = new Car("Lena", "sigma", 2015, 150, info3);
    Car c4 = new Car("AMG", "A7", 2010, 130, info4);
    Car c5 = new Car("Tesla", "T1", 2011, 90, info5);

    log("Part 0. insert cars");
    repo.save(c1);//from   ww w.ja  va2s .  co  m
    repo.save(c2);
    repo.save(c3);
    repo.save(c4);
    repo.save(c5);

    Iterator<Car> it = repo.findAll().iterator();
    log("Part 0. result of inserted car: ");
    while (it.hasNext()) {
        log(it.next().toString());
    }

    log("Part 1. findByOrderByMakerAsc");
    it = repo.findByOrderByMakerAsc().iterator();
    log("Result: ");
    while (it.hasNext()) {
        log(it.next().toString());
    }

    log("Part 2. findByLicenseInfoOwnerName");
    it = repo.findByLicenseInfoOwnerName("JD").iterator();
    log("Result: ");
    while (it.hasNext()) {
        log(it.next().toString());
    }

    log("Part 3. findByMaker");
    it = repo.findByMaker("AMG").iterator();
    log("Result: ");
    while (it.hasNext()) {
        log(it.next().toString());
    }

    log("Part 4. findByMakeYearBetween");
    it = repo.findByMakeYearBetween(2010, 2013).iterator();
    log("Result: ");
    while (it.hasNext()) {
        log(it.next().toString());
    }

    log("Part 5. findByLicenseInfoLicensePlateNumber");
    Car c = repo.findByLicenseInfoLicensePlateNumber("HL9010");
    log("Result: ");
    log(c.toString());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    User a = new User("A", "B");
    System.out.println("logon a = " + a);
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("User.out"));
    o.writeObject(a);//from   ww  w.  ja  v a 2s .co  m
    o.close();

    Thread.sleep(1000); // Delay for 1 second

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("User.out"));
    System.out.println("Recovering object at " + new Date());
    a = (User) in.readObject();
    System.out.println("logon a = " + a);
}

From source file:TimeTrial.java

public static void main(String[] args) {

    StopWatch stWatch = new StopWatch();

    // Start StopWatch
    stWatch.start();/*  ww w  . j a  va 2s  .co  m*/

    // Get iterator for all days in a week starting Monday
    Iterator itr = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_MONDAY);

    while (itr.hasNext()) {
        Calendar gCal = (Calendar) itr.next();
        System.out.println(gCal.getTime());
    }

    // Stop StopWatch
    stWatch.stop();
    System.out.println("Time Taken >>" + stWatch.getTime());

}