How do you get the time in Java?
Java Time
Original Unix represented time as number of seconds since 1/1/70 00:00 GMT
Java represents time in milliseconds since January 1, 1970, 12:00 am GMT
Java uses a long (Java-based apps can represent dates ±292 million years)
public class Now {
/**
* Simple program to print milliseconds since January 1, 1970.
* @param args arguments
*/
public static void main(String[] args) {
long now = System.currentTimeMillis();
System.out.println(now);
}
}
E.g. DecimalFormat
java.text.DecimalFormat df;
df = new java.text.DecimalFormat("#,###");
E.g. Date Class
public class Now3 {
/**
* Simple program to use Date in java.util package.
* @param args arguments
*/
public static void main(String[] args) {
java.util.Date d = new java.util.Date();
System.out.println(d);
}
}
E.g. Calendar Class
Calendar c = Calendar.getInstance();
E.g. SimpleDateFormat
public class Now5 {
/**
* Simple program to print date using SimpleDateFormat.
* @param args arguments
*/
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy @ HH:mm");
Date d = new Date();
System.out.println(sdf.format(d));
}
}
* Be careful with Months and minutes
E.g. Locale and DateFormat
public class Now6 {
/**
* Simple program to use Date along with Locale and DateFormat (Medium).
* @param args arguments
*/
public static void main(String[] args) {
String languageCode = args[0];
Locale locale = new Locale(languageCode);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
Date d = new Date();
System.out.println(df.format(d));
}
}
E.g. DateFormat(Long)
Mutable vs. Immutable
Immutable object is an object whose state cannot be modified after it is created (instantiated)
E.g. java.lang.String
Mutable object is an object whose state cannot be modified after it is created (instantiated)
E.g. java.lang.StringBuilder, java.util.Date, java.util.Calendar
When you use a class in Java, how does Java know:
- Whether you wrote it?
- Whether it' s in the class library?
- What methods, etc, the class offers?
CLASSPATH
Java maps the class name into a file name
Java searches for this file down the CLASSPATH (environment variable)
- If not specified, checks current dir & location of the JVM
Java Archives (JAR files) can also be searched
Cookbook programming
You have a template for your program
You change things around, but you don't mess with the overall structure
Many people consider Swing development to be cookbook programming
Graphical User Interface (GUI)
A graphical window or a system of graphical windows
Presented by an application for interacting with the user such as input from keyborard and mouse clicks
Java Foundation Classes (JFC) has many sets of classes
- AWT (Abstract Window Toolkit) in java.awt package
- Swing in javax.swing package
Swing (javax.swing)
A user interface environment
- Implemented in Java: More consistent accross platforms
- Offers different "look and feel" options: Windows, Unix, and other
- Can be a standalone app or a JApplet
Still uses AWT for event handing, layouts, fonts, etc
JavaFX is also an option but we will use Swing
Simplest Structure
1. Make a Window
2. Make a Container
3. Add your components such as Buttons, TextFields, etc. to the Container
4. Set up the Window to display the Container
JFrame & JPanel
JFrame is the Swing Window (Initially, not visible)
JPanel (aka a pane) is the Container to which you add your components (or other containers)
- Set JFrame's content pane to be JPanel
Components
JLabel, JButton, JTextField, JTextArea, JCheckBox, JList, JChoice, JScrollBar, JRadioButton, and more
Layout Managers
Object that governs positions and sizes of components in a container
The default Layout Manager for JPanel object is FlowLayout
- Place items in the container from left to right
- When a line is full, components flow to the next row
More Layout Options
BorderLayout
- Places components in up to five areas: top(north), bottom(south), left(west), right(east), center
GridLayout
- Makes components equal in size
- Displays them in the requested rows and columns
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* Example code for lecture 9.
* @author Jeffrey Eppinger & Terry Lee
*/
public class QuoteGUI2 {
/**
* Constructor where JFrame and other components are instantiated.
*/
public QuoteGUI2() {
JFrame frame = new JFrame("08-671 QuoteGUI Example");
frame.setSize(520, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
Font bigFont = new Font(Font.SERIF, Font.BOLD, 20);
JLabel label = new JLabel("How about a presidential quote?");
label.setFont(bigFont);
pane.add(label);
JPanel line = new JPanel();
JButton button = new JButton(new ImageIcon("obama.jpg"));
line.add(button);
JButton button2 = new JButton(new ImageIcon("trump.jpg"));
line.add(button2);
pane.add(line);
JTextArea area = new JTextArea(10, 40);
area.setText("Yes, we can.");
pane.add(area);
frame.setContentPane(pane);
frame.setVisible(true);
}
/**
* Simple program to show cookbook programming for Swing.
* @param args arguments
*/
public static void main(String[] args) {
new QuoteGUI2();
}
}