Japanese Calendar : Calendar « I18N « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » I18N » Calendar 
13.9.1.Japanese CalendarPrevious/Next
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JapaneseCalendar extends JPanel {
  public JapaneseCalendar() {
    setLayout(new BorderLayout());
    final CalendarPane cp = new CalendarPane(Locale.JAPANESE);
    add(cp, BorderLayout.NORTH);
  }

  public static void main(String[] a) {
    JFrame f = new JFrame();

    f.add(new JapaneseCalendar());
    f.pack();
    f.setVisible(true);
  }
}

class CalendarPane extends JPanel {
  final static Locale JAPAN_IMP_ERA = new Locale("ja""JP""JP");

  private Locale locale;
  CalendarPane(Locale initLocale) {
    setPreferredSize(new Dimension(400400));
    locale = initLocale;
  }

  public void paintComponent(Graphics g) {
    int width = 400;
    int height = 400;

    Calendar cal = Calendar.getInstance(locale);
    cal.setTime(new Date());

    String header = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, locale);
    header += " " + cal.get(Calendar.YEAR);

    FontMetrics fm = g.getFontMetrics();
    Insets insets = getInsets();
    g.setColor(Color.black);
    g.drawString(header, (width - fm.stringWidth(header)) 2, insets.top + fm.getHeight());

    DateFormatSymbols dfs = new DateFormatSymbols(locale);
    String[] weekdayNames = dfs.getShortWeekdays();
    int fieldWidth = (width - insets.left - insets.right7;
    g.drawString(weekdayNames[Calendar.SUNDAY], insets.left
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SUNDAY])) 2, insets.top + 3
        * fm.getHeight());
    g.drawString(weekdayNames[Calendar.MONDAY], insets.left + fieldWidth
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.MONDAY])) 2, insets.top + 3
        * fm.getHeight());
    g.drawString(weekdayNames[Calendar.TUESDAY], insets.left + * fieldWidth
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.TUESDAY])) 2, insets.top + 3
        * fm.getHeight());
    g.drawString(weekdayNames[Calendar.WEDNESDAY], insets.left + * fieldWidth
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.WEDNESDAY])) 2, insets.top + 3
        * fm.getHeight());
    g.drawString(weekdayNames[Calendar.THURSDAY], insets.left + * fieldWidth
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.THURSDAY])) 2, insets.top + 3
        * fm.getHeight());
    g.drawString(weekdayNames[Calendar.FRIDAY], insets.left + * fieldWidth
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.FRIDAY])) 2, insets.top + 3
        * fm.getHeight());
    g.drawString(weekdayNames[Calendar.SATURDAY], insets.left + * fieldWidth
        (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SATURDAY])) 2, insets.top + 3
        * fm.getHeight());

    int dom = cal.get(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int col = 0;
    switch (cal.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY:
      col = 1;
      break;

    case Calendar.TUESDAY:
      col = 2;
      break;

    case Calendar.WEDNESDAY:
      col = 3;
      break;

    case Calendar.THURSDAY:
      col = 4;
      break;

    case Calendar.FRIDAY:
      col = 5;
      break;

    case Calendar.SATURDAY:
      col = 6;
    }
    cal.set(Calendar.DAY_OF_MONTH, dom);

    int row = * fm.getHeight();
    for (int i = 1; i <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
      g.drawString("" + i, insets.left + fieldWidth * col + (fieldWidth - fm.stringWidth("" + i))
          2, row);
      if (++col > 6) {
        col = 0;
        row += fm.getHeight();
      }
    }
  }

}
13.9.Calendar
13.9.1.Japanese Calendar
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.