Parse and format a date in Java

import java.text.DateFormat; import java.text.ParseException; import java.util.Date; public class DateFormatTest { public static void main(String[] args) { DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);   DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM); DateFormat longDf = DateFormat.getDateInstance(DateFormat.LONG); DateFormat fullDf = DateFormat.getDateInstance(DateFormat.FULL); System.out.println(shortDf.format(new Date())); System.out.println(mediumDf.format(new Date())); System.out.println(longDf.format(new Date())); System.out.println(fullDf.format(new Date())); // parsing try { Date date = shortDf.parse("12/12/2010"); } catch (ParseException e) { } […]
Continue reading…

Enjoyed this post? Share it!

 
 

After and before methods in Java date class

The after method returns true if this date is a later time than the when argument. Otherwise, it returns false. The before method returns true if this date is before the specified date and returns false otherwise. The following code prints “date1 before date2” because the first date represents a time that is one millisecond […]
Continue reading…

Enjoyed this post? Share it!

 
 

Parsing numbers in Java

The example below takes user input and parses it. If the user types in an invalid number, an error message will be displayed. import java.util.Scanner; public class NumberTest { public static void main(String[] args) {   Scanner scanner = new Scanner(System.in); String userInput = scanner.next(); try { int i = Integer.parseInt(userInput); System.out.println("The number entered: " […]
Continue reading…

Enjoyed this post? Share it!

 
 

Methods in the Math class in Java

public static double abs(double a) Returns the absolute value of the specified double.. public static double acos(double a) Returns the arc cosine of an angle, in the range of 0.0 through pi. public static double asin(double a) Returns the arc sine of an angle, in the range of –pi/2 through pi/2. public static double atan(double […]
Continue reading…

Enjoyed this post? Share it!