Tutorial by Examples: d

A simple example of producer-consumer problem solution. Notice that JDK classes (AtomicBoolean and BlockingQueue) are used for synchronization, which reduces the chance of creating an invalid solution. Consult Javadoc for various types of BlockingQueue; choosing different implementation may drastica...
Date date = new Date(); System.out.println(date); // Thu Feb 25 05:03:59 IST 2016 Here this Date object contains the current date and time when this object was created. Calendar calendar = Calendar.getInstance(); calendar.set(90, Calendar.DECEMBER, 11); Date myBirthDate = calendar.getTime(); ...
Calendar, Date, and LocalDate Java SE 8 before, after, compareTo and equals methods //Use of Calendar and Date objects final Date today = new Date(); final Calendar calendar = Calendar.getInstance(); calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); Date birthdate = calendar.getTime(); ...
format() from SimpleDateFormat class helps to convert a Date object into certain format String object by using the supplied pattern string. Date today = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); //pattern is specified here System.out.println(date...
parse() from SimpleDateFormat class helps to convert a String pattern into a Date object. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); String dateStr = "02/25/2016"; // input String Date date = dateFormat.parse(dateStr); System.out.println(date.getYe...
Calendar objects can be created by using getInstance() or by using the constructor GregorianCalendar. It's important to notice that months in Calendar are zero based, which means that JANUARY is represented by an int value 0. In order to provide a better code, always use Calendar constants, such as...
add() and roll() can be used to increase/decrease Calendar fields. Calendar calendar = new GregorianCalendar(2016, Calendar.MARCH, 31); // 31 March 2016 The add() method affects all fields, and behaves effectively if one were to add or subtract actual dates from the calendar calendar.add(Calend...
When desired XML format differs from Java object model, an XmlAdapter implementation can be used to transform model object into xml-format object and vice versa. This example demonstrates how to put a field's value into an attribute of an element with field's name. public class XmlAdapterExample { ...
The getClass() method can be used to find the runtime class type of an object. See the example below: public class User { private long userID; private String name; public User(long userID, String name) { this.userID = userID; this.name = name; } } pu...
Static and member methods in Java can be marked as native to indicate that their implementation is to be found in a shared library file. Upon execution of a native method, the JVM looks for a corresponding function in loaded libraries (see Loading native libraries), using a simple name mangling sche...
Calling a Java method from native code is a two-step process : obtain a method pointer with the GetMethodID JNI function, using the method name and descriptor ; call one of the Call*Method functions listed here. Java code /*** com.example.jni.JNIJavaCallback.java ***/ package com.example....
Add the dependency as described in the Remark section, then add a RecyclerView to your layout: <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> ...
This Activity code will provide basic functionality for including a Google Map using a SupportMapFragment. The Google Maps V2 API includes an all-new way to load maps. Activities now have to implement the OnMapReadyCallBack interface, which comes with a onMapReady() method override that is execute...
To add markers to a Google Map, for example from an ArrayList of MyLocation Objects, we can do it this way. The MyLocation holder class: public class MyLocation { LatLng latLng; String title; String snippet; } Here is a method that would take a list of MyLocation Objects and place a M...
The shared remote interface: package remote; import java.rmi.Remote; import java.rmi.RemoteException; public interface RemoteServer extends Remote { int stringToInt(String string) throws RemoteException; } The server implementing the shared remote interface: package server; im...
Annotation @XmlAccessorType determines whether fields/properties will be automatically serialized to XML. Note, that field and method annotations @XmlElement, @XmlAttribute or @XmlTransient take precedence over the default settings. public class XmlAccessTypeExample { @XmlAccessorType(XmlAccessT...
Annotations @XmlElement, @XmlAttribute or @XmlTransient and other in package javax.xml.bind.annotation allow the programmer to specify which and how marked fields or properties should be serialized. @XmlAccessorType(XmlAccessType.NONE) // we want no automatic field/property marshalling public cla...
WebView wv = new WebView(); WebEngine we = wv.getEngine(); we.load("https://stackoverflow.com"); WebView is the UI shell around the WebEngine. Nearly all controls for non UI interaction with a page are done through the WebEngine class.
import static java.awt.BorderLayout.*; import javax.swing.*; import java.awt.BorderLayout; JPanel root = new JPanel(new BorderLayout()); root.add(new JButton("East"), EAST); root.add(new JButton("West"), WEST); root.add(new JButton("North"), NORTH); root.add(...
PrinterJob pJ = PrinterJob.createPrinterJob(); if (pJ != null) { boolean success = pJ.showPrintDialog(primaryStage);// this is the important line if (success) { pJ.endJob(); } }

Page 12 of 691