In this section you'll learn the most common methods to format Dates, Numbers and Currencies as well as the relationship between the main classes responsible to do that.
Let's start with the classes that you have to learn.
java.util.Date;
java.util.Calendar;
java.text.DateFormat;
java.text.NumberFormat;
java.util.Locale;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.formatting; | |
import java.util.Date; | |
public class AppDate { | |
public static void main(String[] args) { | |
Date d = new Date(); | |
System.out.println(d.toString()); | |
Date d2 = new Date(1000000L); | |
System.out.println(d2.toString()); | |
Date d3 = new Date(); | |
System.out.println(d3.getTime()); | |
Date d4 = new Date(d3.getTime()); | |
System.out.println(d4.toString()); | |
Date d5 = new Date(); | |
d5.setTime(10000054700104L); | |
System.out.println(d5.toString()); | |
} | |
} |
Console:
Wed Jul 18 10:18:42 AMT 2012 |
Wed Dec 31 19:16:40 AMT 1969 |
1342621122734 |
Wed Jul 18 10:18:42 AMT 2012 |
Sun Nov 21 04:58:20 AMT 2286 |
The class java.util.Date is hard to work with,if you look at the preceding example to set a specific date we have to use a number representation(long), this class is normally used to create a date that represents "now" as well as working with java.util.Calendar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.formatting; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class AppCalendarDate { | |
public static void main(String[] args) { | |
Date d1 = new Date("09/15/2012"); | |
System.out.println(d1.toString()); | |
Date d = new Date(); | |
System.out.println("Today is " + d.toString()); | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(d); | |
System.out.println("Day of the week " + cal.get(Calendar.DAY_OF_WEEK)); | |
System.out.println("Day of the year " + cal.get(Calendar.DAY_OF_YEAR)); | |
Calendar c1 = Calendar.getInstance(); | |
c1.setTime(d1); | |
System.out.println("Last day of the year " + c1.get(Calendar.DAY_OF_YEAR)); | |
long current = cal.get(Calendar.DAY_OF_YEAR); | |
long last = c1.get(Calendar.DAY_OF_YEAR); | |
long remaining = last - current; | |
System.out.println("Remaining Days ==> " + remaining ); | |
cal.add(Calendar.YEAR, 2); | |
d = cal.getTime(); | |
System.out.println(d.toString()); | |
cal.roll(Calendar.MONTH, 10); | |
d = cal.getTime(); | |
System.out.println(d.toString()); | |
} | |
} |
Console:
Sat Sep 15 00:00:00 AMT 2012
Today is Wed Jul 18 10:37:26 AMT 2012
Day of the week 4
Day of the year 200
Last day of the year 259
Remaining Days ==> 59
Fri Jul 18 10:37:26 AMT 2014
Sun May 18 10:37:26 AMT 2014
Watch out, the class Calendar is abstract you cannot create an instance of it(look at line 16), also it provides friendly methods to manipulate dates, where you can add hours, days, weeks, and so on.
Notice the class java.util.Calendar work together java.util.Date, instead of setting a date using number(long) you can use a Date object.
Finally the method roll is similar to add, the difference is it does not change into next month(if you're adding days), years(if you're adding months) and so on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.formatting; | |
import java.text.DateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Locale; | |
public class AppDateLocale { | |
public static void main(String[] args) { | |
Calendar c = Calendar.getInstance(); | |
c.set(2012, 07, 17); | |
Date d = c.getTime(); | |
Locale us = new Locale("en"); | |
Locale pt = new Locale("pt","br"); | |
Locale fr = new Locale("fr"); | |
Locale sp = new Locale("es"); | |
Locale ja = new Locale("ja"); | |
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, us); | |
DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL, pt); | |
DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, fr); | |
DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL, sp); | |
DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL, ja); | |
System.out.println(df.format(d)); | |
System.out.println(df1.format(d)); | |
System.out.println(df2.format(d)); | |
System.out.println(df3.format(d)); | |
System.out.println(df4.format(d)); | |
try { | |
String date = df.format(d); | |
Date parseDate = df.parse(date); | |
System.out.println("Parse ==> " + parseDate); | |
} catch ( ParseException e){} | |
} | |
} |
Console:
Friday, August 17, 2012
Sexta-feira, 17 de Agosto de 2012
vendredi 17 août 2012
viernes 17 de agosto de 2012
2012?8?17?
Parse ==> Fri Aug 17 00:00:00 AMT 2012
The class java.text.DateFormat is an abstract class that provides us formatted dates using or not pré-defined styles as well as Locales.
You can get a DateFormat class by invoking the methods getInstance() and getDateInstance().
Finally we can parse a String to Date by using the method parse() which must be enclosed in a try-catch block.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.formatting; | |
import java.util.Locale; | |
public class AppLocale { | |
public static void main(String[] args) { | |
Locale[] loc = new Locale[4]; | |
loc[0] = new Locale("pt"); | |
loc[1] = new Locale("pt","br"); | |
loc[2] = new Locale("fr"); | |
loc[3] = new Locale("en","us"); | |
for ( Locale l : loc) { | |
System.out.print(l.getLanguage() + " -> "); | |
System.out.println(l.getDisplayCountry(l)); | |
} | |
} | |
} |
Console:
pt ->
pt -> Brasil
fr ->
en -> United States
Let's take a briefly look at java.util.Locale.
The first argument is related to the language and the second is a country.
The most important methods are getLanguage() and getDisplayCountry().
There are not setters to define language and country at java.util.Locale.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.formatting; | |
import java.text.NumberFormat; | |
import java.text.ParseException; | |
import java.util.Locale; | |
public class AppNumberFormat { | |
public static void main(String[] args) { | |
float f = 1100.56845f; | |
Locale pt = new Locale("pt"); | |
Locale us = new Locale("en","us"); | |
NumberFormat nf[] = new NumberFormat[6]; | |
nf[0] = NumberFormat.getNumberInstance(); | |
nf[0].setMaximumFractionDigits(5); | |
nf[1] = NumberFormat.getNumberInstance(pt); | |
nf[2] = NumberFormat.getNumberInstance(us); | |
nf[3] = NumberFormat.getCurrencyInstance(); | |
nf[4] = NumberFormat.getCurrencyInstance(pt); | |
nf[5] = NumberFormat.getCurrencyInstance(us); | |
for ( NumberFormat nfs : nf ) { | |
System.out.println(nfs.format(f)); | |
} | |
System.out.println(nf[0].getMaximumFractionDigits()); | |
try { | |
NumberFormat nf1 = NumberFormat.getInstance(); | |
System.out.println(nf1.parse("1234.567")); | |
nf1.setParseIntegerOnly(true); | |
System.out.println(nf1.parse("1234.567")); | |
} catch (ParseException pe) { | |
System.out.println("parse exc"); | |
} | |
} | |
} |
Console:
1.100,56848
1.100,568
1,100.568
R$ 1.100,57
¤ 1.100,57
$1,100.57
5
1234567
1234567
Similar to java.text.DateFormat the class java.text.NumberFormat works with numbers(currency, percent, and so on), where there are many ways to get a concrete class since NumberFormat is abstract.
getInstance(),
getNumberInstance(),
getCurrencyInstance(),
.. and so on
The method parse() is present as well where you can get a Number object.
No comments:
Post a Comment