Skip to content

Instantly share code, notes, and snippets.

@eduzera
Created January 11, 2021 19:35
Show Gist options
  • Save eduzera/b3418f1cfa8502369e51d424f34f2167 to your computer and use it in GitHub Desktop.
Save eduzera/b3418f1cfa8502369e51d424f34f2167 to your computer and use it in GitHub Desktop.
Java Date abd Currency Format
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
public class main {
public static void main(String[] args){
// 1. Current Date
Date date = new Date();
// 2. Create locales pt-BR and US
Locale brazilLocale = new Locale("pt", "BR");
Locale usLocale = new Locale("en", "US");
// 3. Display Date short format
DateFormat dfBR = new SimpleDateFormat("DD/MM/YYYY HH:mm", brazilLocale);
DateFormat dfUS = new SimpleDateFormat("MM/DD/YYYY hh:mm aa", usLocale);
System.out.println("-----");
System.out.println("[BR] " + dfBR.format(date));
System.out.println("[US] " + dfUS.format(date));
System.out.println("-----");
// 04 Display Date medium format
dfBR = new SimpleDateFormat("DD MMM - HH:mm", brazilLocale);
dfUS = new SimpleDateFormat("MMM DD - hh:mm aa", usLocale);
System.out.println("[BR] " + dfBR.format(date));
System.out.println("[US] " + dfUS.format(date));
System.out.println("-----");
// 05 Display Date large format
dfBR = new SimpleDateFormat("EEEE DD MMM 'às' HH:mm", brazilLocale);
dfUS = new SimpleDateFormat("EEEE DD MMM 'at' hh:mm aa", usLocale);
System.out.println("[BR] " + dfBR.format(date));
System.out.println("[US] " + dfUS.format(date));
System.out.println("-----");
// 06 Display Currency Format
double[] numbers = {1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0};
NumberFormat nfBR = NumberFormat.getCurrencyInstance(brazilLocale);
NumberFormat nfUS = NumberFormat.getCurrencyInstance(usLocale);
for(double number : numbers){
System.out.println("[BR] " + nfBR.format(number));
System.out.println("[US] " + nfUS.format(number));
System.out.println("-----");
}
}
}
-----
[BR] 11/01/2021 17:30
[US] 01/11/2021 05:30 PM
-----
[BR] 11 jan - 17:30
[US] Jan 11 - 05:30 PM
-----
[BR] Segunda-feira 11 jan às 17:30
[US] Monday 11 Jan at 05:30 PM
-----
[BR] R$ 1,00
[US] $1.00
-----
[BR] R$ 10,00
[US] $10.00
-----
[BR] R$ 100,00
[US] $100.00
-----
[BR] R$ 1.000,00
[US] $1,000.00
-----
[BR] R$ 10.000,00
[US] $10,000.00
-----
[BR] R$ 100.000,00
[US] $100,000.00
-----
[BR] R$ 1.000.000,00
[US] $1,000,000.00
-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment