JAVA代码便遍历当前月 java iterator遍历

java 中能够实现年月循环的代码import java.util.Calendar;
public class MainTester {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
//设置为12月31日
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DAY_OF_MONTH, 31);
System.out.println("现在日期:"calendar.getTime());
//再增加一天
calendar.add(Calendar.DAY_OF_MONTH, 1);
System.out.println("增加后的日期:"calendar.getTime());
}
}
使用Calendar就可以实现,很简单,程序输出为:
现在日期:Tue Dec 31 11:50:55 CST 2013
增加后的日期:Wed Jan 01 11:50:55 CST 2014
如何在java中实现List集合的遍历java中实现List集合的遍历有三种方法:
方法一:for-each循环
for(String attribute : list) {
System.out.println(attribute);
}
方法二:for循环
for(int i = 0 ; ilist.size() ; i) {
system.out.println(list.get(i));
}
方法三:迭代器迭代
Iterator it = list.iterator();
while(it.hasNext()) {
System.ou.println(it.next);
}
java 某段时间内遍历出啊所有的年度 季度 月度 日 以及日期这样可以吗 JAVA代码便遍历当前月,输入年份和月份后就会显示那年那月JAVA代码便遍历当前月的第一天到最后一天是星期几
import java.util.Scanner;
public class Calendar{
public static void main(String[] args){
Scanner x=new Scanner(System.in);
System.out.println("请输入年:");
int nian=x.nextInt();
System.out.println("请输入月:");
int yue=x.nextInt();
int zong=0;
for(int year=0001;year=nian-1;year){
if(year%4==0year0!=0||year@0==0){
zong =366;
}
else{
zong =365;
}
}
for(int month=1;month=yue-1;month){
if(month==2){
if(nian%4==0nian0!=0||nian@0==0){
zong =29;
}
else{
zong =28;
}
}
else if(month==4||month==6||month==9||month==11){
zong =30;
}
else{
zong =31;
}
}
zong =1;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1;i=zong%7;i){
System.out.print("\t");
}
if(yue==2){
if(nian%4==0nian0!=0||nian@0==0){
for(int i=1;i=29;i){
if(zong%7==6){
System.out.print(i "\n");
}
else{
System.out.print(i "\t");
}
zong;
}
}
else{
for(int i=1;i=28;i){
if(zong%7==6){
System.out.print(i "\n");
}
else{System.out.print(i "\t");
}
zong;
}
}
}
else if(yue==4||yue==6||yue==9||yue==11){
for(int i=1;i=30;i){
if(zong%7==6){
System.out.print(i "\n");
}
else{System.out.print(i "\t");
}
zong;
}
}
else{
for(int i=1;i=31;i){
if(zong%7==6){
System.out.print(i "\n");
}
else{System.out.print(i "\t");
}
zong;
}
}
}
}
这段循环遍历时间的java代码,怎么改才是正确的呢?你把天数加上1的上面那一行代码注释取消掉改成 System.out.println(sdf.format(tmp)); 就行JAVA代码便遍历当前月了啊....
你要显示每一天 那肯定得把打印代码写在循环里面呀 或者在循环里面把每一天都拼接在一起 最后return JAVA代码便遍历当前月 , 你目前把循环输出给注释掉了JAVA代码便遍历当前月,然后又只return了最后一天的字符串回去 那当然只打印最后一天 。
你要不就把那行代码取消了 JAVA代码便遍历当前月 , 要不就先声明一个String用来接收循环拼接完的日期最后return回去
用java控制台实现万年历-要求打印当前年月日的日历表格,要求对当天的日期单独标示以下是用java swing编写的日历,很好用 , 在我所做的系统里就能够正常的使用
接下来 是具体代码:
package Demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DateChooser extends JPanel implements ActionListener,
ChangeListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int startYear = 1980;
int lastYear = 2050;
int width = 270;
int height = 200;
Color backGroundColor = Color.gray;
Color palletTableColor = Color.white;
Color todayBackColor = Color.orange;
Color weekFontColor = Color.blue;
Color dateFontColor = Color.black;
Color weekendFontColor = Color.red;
Color controlLineColor = Color.pink;
Color controlTextColor = Color.white;
Color rbFontColor = Color.white;
Color rbBorderColor = Color.red;
Color rbButtonColor = Color.pink;
Color rbBtFontColor = Color.red;
JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JSpinner minuteSpin;
JButton[][] daysButton = new JButton[6][7];
JFormattedTextField jFormattedTextField;
Calendar c = getCalendar();
DateChooser(JFormattedTextField jftf) {
jFormattedTextField = jftf;
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);
}
private JPanel createYearAndMonthPanal() {
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)1;
int currentHour = c.get(Calendar.HOUR_OF_DAY);
int currentMinute = c.get(Calendar.MINUTE);
JPanel result = new JPanel();
result.setLayout(new FlowLayout());
result.setBackground(controlLineColor);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear,
lastYear, 1));
yearSpin.setPreferredSize(new Dimension(48, 20));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
result.add(yearSpin);
JLabel yearLabel = new JLabel("年");
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1));
monthSpin.setPreferredSize(new Dimension(35, 20));
monthSpin.setName("Month");
monthSpin.addChangeListener(this);
result.add(monthSpin);
JLabel monthLabel = new JLabel("月");
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);
hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23, 1));
hourSpin.setPreferredSize(new Dimension(35, 20));
hourSpin.setName("Hour");
hourSpin.addChangeListener(this);
result.add(hourSpin);
JLabel hourLabel = new JLabel("时");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
minuteSpin = new JSpinner(new SpinnerNumberModel(currentMinute, 0, 59,
1));
minuteSpin.setPreferredSize(new Dimension(35, 20));
minuteSpin.setName("Minute");
minuteSpin.addChangeListener(this);
result.add(minuteSpin);
JLabel minuteLabel = new JLabel("分");
minuteLabel.setForeground(controlTextColor);
result.add(minuteLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
result.setFont(new Font("宋体", Font.PLAIN, 12));
result.setLayout(new GridLayout(7, 7));
result.setBackground(Color.white);
JLabel cell;
for (int i = 0; i7; i) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.CENTER);
if (i == 0 || i == 6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
}
int actionCommandId = 0;
for (int i = 0; i6; i)
for (int j = 0; j7; j) {
JButton numberButton = new JButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.CENTER);
numberButton.setActionCommand(String.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if (j == 0 || j == 6)
numberButton.setForeground(weekendFontColor);
else
numberButton.setForeground(dateFontColor);
daysButton[i][j] = numberButton;
numberButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
closeAndSetDate();
}
}
});
result.add(numberButton);
actionCommandId;
}
return result;
}
private JDialog createDialog(Frame owner) {
JDialog result = new JDialog(owner, "日期时间选择", true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this, BorderLayout.CENTER);
result.pack();
result.setSize(width, height);
return result;
}
public void showDateChooser(Point position) {
Object tmpobj=SwingUtilities.getWindowAncestor(jFormattedTextField);
if(tmpobj.getClass().isInstance(new JDialog())||tmpobj.getClass().getSuperclass().isInstance(new JDialog()))
{
JDialog ownerdialog = (JDialog) SwingUtilities
.getWindowAncestor(jFormattedTextField);
Frame owner = (Frame) SwingUtilities.getWindowAncestor(ownerdialog);
if (dialog == null || dialog.getOwner() != owner) {
dialog = createDialog(owner);
}
dialog.setLocation(getAppropriateLocation(owner, position));
}
else if(tmpobj.getClass().isInstance(new JFrame())||tmpobj.getClass().getSuperclass().isInstance(new JFrame()))
{
JFrame ownerFrame = (JFrame) SwingUtilities
.getWindowAncestor(jFormattedTextField);
if (dialog == null || dialog.getOwner() != ownerFrame) {
dialog = createDialog(ownerFrame);
【JAVA代码便遍历当前月 java iterator遍历】}
dialog.setLocation(getAppropriateLocation(ownerFrame, position));
}
flushWeekAndDay();
dialog.setVisible(true);
}
Point getAppropriateLocation(Frame owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.xwidth) - (p.xowner.getWidth());
int offsetY = (position.yheight) - (p.yowner.getHeight());
if (offsetX0) {
result.x -= offsetX;
}
if (offsetY0) {
result.y -= offsetY;
}
return result;
}
public void closeAndSetDate() {
setDate(c.getTime());
dialog.dispose();
}
private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate());
return result;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
private int getSelectedHour() {
return ((Integer) hourSpin.getValue()).intValue();
}
private int getSelectedMinute() {
return ((Integer) minuteSpin.getValue()).intValue();
}
private void dayColorUpdate(boolean isOldDay) {
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, 1);
int actionCommandId = day - 2c.get(Calendar.DAY_OF_WEEK);
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay)
daysButton[i][j].setForeground(dateFontColor);
else
daysButton[i][j].setForeground(todayBackColor);
}
private void flushWeekAndDay() {
c.set(Calendar.DAY_OF_MONTH, 1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i6; i) {
for (int j = 0; j7; j) {
String s = "";
if (dayNo = 1dayNo = maxDayNo)
s = String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo;
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) e.getSource();
if (source.getName().equals("Minute")) {
c.set(Calendar.MINUTE, getSelectedMinute());
return;
}
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
return;
}
dayColorUpdate(true);
if (source.getName().equals("Year")) {
c.set(Calendar.YEAR, getSelectedYear());
}
if (source.getName().equals("Month")) {
c.set(Calendar.MONTH, getSelectedMonth() - 1);
}
flushWeekAndDay();
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source.getText().length() == 0)
return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
c.set(Calendar.DAY_OF_MONTH, newDay);
}
public void setDate(Date date) {
jFormattedTextField.setText(getDefaultDateFormat().format(date));
}
public Date getDate() {
try {
String dateString = jFormattedTextField.getText();
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
} catch (Exception ee) {
return getNowDate();
}
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy-MM-ddHH:mm");
}
}
测试程序:
package Demo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DemoForDateChooser extends JFrame {
private JFormattedTextField formattedTextField;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public DemoForDateChooser() {
super();
setTitle("日期选择框");
getContentPane().setLayout(null);
setBounds(100, 100, 383, 137);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 375, 107);
getContentPane().add(panel);
formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(68, 48, 175, 23);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateChooser mDateChooser=new DateChooser(formattedTextField);
Point p = button.getLocationOnScreen();
p.y = p.y30;
mDateChooser.showDateChooser(p);
formattedTextField.requestFocusInWindow();
}
});
button.setText("选择日期");
button.setBounds(249, 47, 99, 23);
panel.add(button);
final JLabel label = new JLabel();
label.setText("日期:");
label.setBounds(10, 47, 71, 23);
panel.add(label);
panel.add(formattedTextField);
//
}
}
在JAVA中怎么获取当前时间的月份 。并转换成int型在JAVA中获取当前时间的月份并转换成int型可以采用Calendar类提供的方法进行 。
具体代码如下:
Calendar calendar=Calendar.getInstance();
//获得当前时间的月份,月份从0开始所以结果要加1
int month=calendar.get(Calendar.MONTH) 1;
关于JAVA代码便遍历当前月和java iterator遍历的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读