BRoy_98

Untitled

Apr 13th, 2021
382
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.time.ZonedDateTime;
  4. import java.time.format.*;
  5. import java.util.Locale;
  6. import java.util.ResourceBundle;
  7.  
  8.  class DateTimeGUIDemo extends JFrame {
  9.     JRadioButton radSys, radPattern;
  10.     JComboBox<String> cmbSys, cmbPattern; //, cmbLanguage;
  11.      JComboBox<Locale> cmbLanguage;
  12.     JPanel pSys, pPattern, pLang;
  13.     JLabel lblDateTime, lblLanguage;
  14.     ButtonGroup bg;
  15.     String sys = "FULL", pattern = "EE";
  16.     boolean isSys = true;
  17.     Locale defaultLocale;
  18.     ResourceBundle string;
  19.  
  20.  
  21.     public DateTimeGUIDemo(){
  22.         super("Date Time Formatter");
  23.         defaultLocale = Locale.getDefault();
  24.  
  25.         string = ResourceBundle.getBundle("string", defaultLocale);
  26.  
  27.         bg = new ButtonGroup();
  28.         radSys = new JRadioButton("System Defined");
  29.         radPattern = new JRadioButton("Select Pattern");
  30.         bg.add(radPattern);
  31.         bg.add(radSys);
  32.  
  33.         cmbSys = new JComboBox<>(new String[]{"FULL", "MEDIUM", "SHORT", "LONG"});
  34.         cmbPattern = new JComboBox<>(new String[]{"EE, dd-MM-YY","dd/MMMM/YYYY", "EEEE dd MMMM, YYYY", "hh:mm:ss a"});
  35.         cmbLanguage = new JComboBox<>(new Locale[]{new Locale("en"), new Locale("de")});
  36.         lblDateTime = new JLabel("Your Date Time here");
  37.         lblLanguage = new JLabel("Select Language");
  38.         pSys = new JPanel();
  39.         pPattern = new JPanel();
  40.         pLang = new JPanel();
  41.         pSys.add(radSys);
  42.         pSys.add(cmbSys);
  43.         pPattern.add(radPattern);
  44.         pPattern.add(cmbPattern);
  45.         pLang.add(lblLanguage);
  46.         pLang.add(cmbLanguage);
  47.         setLayout(new GridLayout(4,1, 10,10));
  48.         add(pLang);
  49.         add(pSys);
  50.         add(pPattern);
  51.         add(lblDateTime);
  52.  
  53.         radSys.addActionListener((ae)->{
  54.             if(radSys.isSelected()) {
  55.                 isSys = true;
  56.                 cmbPattern.setEnabled(false);
  57.                 cmbSys.setEnabled(true);
  58.             }
  59.         });
  60.         radPattern.addActionListener((ae)->{
  61.             if(radPattern.isSelected()) {
  62.                 isSys = false;
  63.                 cmbPattern.setEnabled(true);
  64.                 cmbSys.setEnabled(false);
  65.             }
  66.         });
  67.  
  68.         cmbLanguage.addActionListener(l -> {
  69.             defaultLocale = (Locale) cmbLanguage.getSelectedItem();
  70.             updateLanguage();
  71.         });
  72.  
  73.         cmbSys.addActionListener(l -> {
  74.             sys = cmbSys.getSelectedItem().toString();
  75.             System.out.println(cmbSys.getSelectedItem());
  76.  
  77.             updateTime();
  78.         });
  79.  
  80.         cmbPattern.addActionListener(l -> {
  81.             pattern = cmbPattern.getSelectedItem().toString();
  82.             System.out.println("pattern: " +  pattern);
  83.  
  84.             updateTime();
  85.         });
  86.  
  87.         radSys.setSelected(true);
  88.         cmbPattern.setEnabled(false);
  89.         cmbSys.setEnabled(true);
  90.  
  91.         updateTime();
  92.     }
  93.  
  94.     public void updateTime() {
  95.         ZonedDateTime dt = ZonedDateTime.now();
  96.  
  97.         // Using Systems predefined formats
  98.         DateTimeFormatter dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
  99.        
  100.         if(isSys) {
  101.             switch (sys) {
  102.                 case "FULL":
  103.                     dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
  104.                     break;
  105.                 case "MEDIUM":
  106.                     dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
  107.                     break;
  108.                 case  "SHORT":
  109.                     dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
  110.                     break;
  111.                 case "LONG":
  112.                     dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
  113.                     break;
  114.                 default:
  115.                     dtf1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
  116.                     break;
  117.             }
  118.         } else {
  119.             dtf1 = DateTimeFormatter.ofPattern(pattern);
  120.         }
  121.  
  122.         lblDateTime.setText(dt.format(dtf1));
  123.  
  124.  
  125.     }
  126.  
  127.  
  128.     public void updateLanguage() {
  129.         string = ResourceBundle.getBundle("string", defaultLocale);
  130.         setTitle(string.getString("title"));
  131.     }
  132.     public static void main(String[] args) {
  133.         DateTimeGUIDemo ob = new DateTimeGUIDemo();
  134.         ob.setSize(500,500);
  135.         ob.setVisible(true);
  136.         ob.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  137.     }
  138. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×