import java.util.Calendar;
成都創新互聯是一家成都做網站、成都網站建設,提供網頁設計,網站設計,網站制作,建網站,按需定制開發,網站開發公司,從2013年開始是互聯行業建設者,服務者。以提升客戶品牌價值為核心業務,全程參與項目的網站策劃設計制作,前端開發,后臺程序制作以及后期項目運營并提出專業建議和思路。
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/**
* Title: Test03.javabr
* Description:
*
* @author 王凱芳
* @date 2020年3月5日 下午6:03:04
* @version 1.0
*
* @request 編寫一個方法能計算任何一個人今天離他最近下一次生日還有多少天,然后在主方法(main方法)中輸入你的出生年月日,調用該方法的計算結果并輸出信息“某某同學離自己最近下一次生日x天”。
*/
public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的姓名:");
String name = sc.nextLine();
System.out.println("請輸入你的生日,格式為(2000/01/01):");
String line = sc.nextLine();
String[] strs = line.split("/");
if (strs.length == 3) {
int days = getDays(strs[0], strs[1], strs[2]);
if (days == 0) {
System.out.println(String.format("%s 同學,今天是你的生日,祝你生日快樂(#^.^#)", name, days));
} else {
System.out.println(String.format("%s 同學離自己最近下一次生日%d天。", name, days));
}
} else {
System.out.println("生日輸入不正確!請按格式輸入。");
}
sc.close();
}
/**
* 獲取最近一次生日天數
*
* @param year
* @param month
* @param day
* @return
*/
public static int getDays(String year, String month, String day) {
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
int now_year = now.get(Calendar.YEAR);
Calendar birthday = Calendar.getInstance();
birthday.set(Calendar.YEAR, now_year);
birthday.set(Calendar.MONTH, Integer.parseInt(month) - 1);
birthday.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
birthday.set(Calendar.HOUR_OF_DAY, 0);
birthday.set(Calendar.MINUTE, 0);
birthday.set(Calendar.SECOND, 0);
birthday.set(Calendar.MILLISECOND, 0);
long diff = now.getTimeInMillis() - birthday.getTimeInMillis();
if (diff == 0) {
return 0;
} else if (diff 0) {
long diffDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
return Math.abs((int) diffDays);
} else {
birthday.add(Calendar.YEAR, 1);
long diffMi = birthday.getTimeInMillis() - now.getTimeInMillis();
long diffDays = TimeUnit.DAYS.convert(diffMi, TimeUnit.MILLISECONDS);
return (int) diffDays;
}
}
}
用JLabel 或Label
我給你寫了個開頭
public class mainFrame extends JFrame{
JLabel jl = new JLabel("用戶名稱:");
JTextField jtf = new JTextField();
JPanel jp_group = new JPanel();
public loginFrame() {
try {
jbInit();common.mysession.dataTrf.getValue("main");
} catch (Exception exception) {
exception.printStackTrace();
}
}
本人急忙做的:有什么不合要求的請再告訴我:
package?show;
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
public?class?Test?extends?JFrame?implements?ActionListener
{
GridBagLayout?g=new?GridBagLayout();
GridBagConstraints?c=new?GridBagConstraints();
Test(String?str)
{
super(str);
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(g);
//調用方法
addComponent();
submit.addActionListener(this);
setVisible(true);
setLocationRelativeTo(null);//設居中顯示;
}
//在這個方法中將會添加所有的組件;
//使用的網格包布局;希望樓主能看懂;
public?void?addComponent()
{
//個人信息登記
noteInformation=new?JLabel("個人信息登記:");
add(g,c,noteInformation,0,0,1,1);
//用戶名
userName=new?JLabel("用戶名:");
add(g,c,userName,0,1,1,1);
//用戶名輸入框
textUserName=new?JTextField(10);
add(g,c,textUserName,1,1,2,1);
//密碼:
password=new?JLabel("密碼:");
add(g,c,password,0,2,1,1);
//密碼輸入框
textUserPassword=new?JTextField(10);
add(g,c,textUserPassword,1,2,2,1);
//性別
sex=new?JLabel("性別:");
add(g,c,sex,0,3,1,1);
//男?女單選框
sexMan=new?JRadioButton("男");
add(g,c,sexMan,1,3,1,1);
sexGirl=new?JRadioButton("女");
add(g,c,sexGirl,2,3,1,1);
ButtonGroup?group=new?ButtonGroup();
group.add(sexMan);
group.add(sexGirl);
//出生日期
birthday=new?JLabel("出生日期:");
add(g,c,birthday,0,4,1,1);
//復選框及其內容
String[]?YEARS=new?String[65];
for(int?i=1950,k=0;i=2014;i++,k++)
{
YEARS[k]=i+"年";
}
year=new?JComboBox(YEARS);
add(g,c,year,1,4,1,1);
//復選框及內容
month=new?JComboBox(MONTH);
add(g,c,month,2,4,1,1);
//submit按鈕
submit=new?JButton("submit");
c.insets=new?Insets(7,0,4,0);
add(g,c,submit,1,5,1,1);
result=new?JTextArea(15,20);
add(g,c,result,0,6,3,4);
}
/* public?void?ActionPerformed(ActionEvent?e)
{
String?s=textUserName.getText();
String?t=textUserPassword.getText();
String?k=sexMan.getText();
String?v=sexGirl.getText();
String?a=(String)?year.getSelectedItem();
String?b=(String)month.getSelectedItem();
String?num="用戶名:"+s+"\n"+"密碼:?"+t+"性別:?"+(k==null?v:k)+"\n"+"出生日期:"+a+"?"+b;
result.append(num);
}*/
public?void?add(GridBagLayout?g,GridBagConstraints?c,JComponent?jc,int?x?,int?y,int?gw,int?gh)
{
c.gridx=x;
c.gridy=y;
c.anchor=GridBagConstraints.WEST;
c.gridwidth=gw;
c.gridheight=gh;
g.setConstraints(jc,c);
add(jc);
}
public?static?void?main(String?args[])
{
new?Test("個人信息登記表");
}
JLabel?noteInformation,userName,password;
JLabel?sex,birthday;
JTextField?textUserName,textUserPassword;
JRadioButton?sexMan,sexGirl;
JComboBox?year,month;
JButton?submit;
JTextArea?result;
final?String[]?MONTH={"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
@Override
public?void?actionPerformed(ActionEvent?arg0)
{
String?s=textUserName.getText();
String?t=textUserPassword.getText();
String?k=sexMan.getText();
String?v=sexGirl.getText();
String?a=(String)?year.getSelectedItem();
String?b=(String)month.getSelectedItem();
String?num="用戶名:"+s+"\n"+"密碼:?"+t+"\n?性別:?"+(k==null?v:k)+"\n"+"出生日期:"+a+"?"+b;
result.setText(num);
}
}
將這段代碼拷貝到UseBirthday類中和你的比較一下看看
public class UseBirthday {
public static void main(String[] args) {
Birthday a=new Birthday(2000,1,1);
a.GetBirthday();
a.Birthday(2008,5,12);
a.GetBirthday();
}
}
class Birthday {
private int Year, Month, Day;
Birthday(){
Year=2000;
Month=1;
Day=1;
}
Birthday(int y,int m,int d) {
Year = y;
Month = m;
Day = d;
}
public void Birthday(int y,int m,int d){
Year = y;
Month = m;
Day = d;
}
int Gety() {
return Year;
}
int Getm() {
return Month;
}
int Getd() {
return Day;
}
void GetBirthday() {
System.out.println(Gety() + "年" + Getm() + "月" + Getd() + "日");
}
}
我當初用struts1和hibernate做了個簡單的!主要的邏輯代碼在下面!數據庫里號碼對應的城市在網上可以查的到
package dao;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import entity.Shenfenzhen;
public class IdDao {
private static Configuration configuration = new Configuration().configure() ;
private static SessionFactory sessionFactory =configuration.buildSessionFactory();
private static Session session = null;
public Info query(String s){
Info info = null;
StringBuffer stringBuffer = new StringBuffer(s);
if(s.length()==18){
String end = stringBuffer.substring(17);
//System.out.println(end);
if(!end.matches("\\d||x")){
return null;
}
}
else if(s.length()==15){
stringBuffer.insert(6, "19");
stringBuffer.append("3");
}
int i = Integer.parseInt(stringBuffer.substring(0, 6));
//System.out.println(i);
session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Shenfenzhen.class);
criteria.add(Expression.eq("num", i));
ListShenfenzhen list = criteria.list();
String address = null;
try {
address = list.get(0).getAddress();
}catch (Exception e) {
return null;
}
i = Integer.parseInt(stringBuffer.substring(16,17));
String sex = null;
if(i%2==0){
sex = "女";
}else{
sex = "男";
}
//System.out.println(i);
stringBuffer = new StringBuffer(stringBuffer.substring(6, 14));
// stringBuffer.insert(4, "-");
// stringBuffer.insert(7, "-");
//System.out.println(stringBuffer);
String ss = stringBuffer.toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String brithday="";
int year=0;
int month =0;
int day =0;
try {
Date date = dateFormat.parse(ss);
//System.out.println(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DAY_OF_MONTH);
brithday = (String)(year + "年" + month + "月" + day + "日");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info = new Info(address, brithday, sex);
Calendar c = Calendar.getInstance();
int nowYear = c.get(Calendar.YEAR);
int nowMonth = c.get(Calendar.MONTH);
int nowDay = c.get(Calendar.DAY_OF_MONTH);
if(nowYearyear){
info = null;
}else if(nowYear ==year nowMonthmonth ){
info = null;
}else if(nowYear == year nowMonth==month nowDayday){
info = null;
}
closeSession(session);
return info;
}
// public static void main(String[] args){
// IdDao dao = new IdDao();
// dao.query("342522998711224513");
// System.out.println("x".matches("\\d||x"));
// }
private void closeSession(Session session){
if(session !=null){
session.close();
}
}
}
import?java.util.*;
public?class?Yugi{
public?static?void?main(String[]?args){
Scanner?scan?=?new?Scanner(System.in);
System.out.println("輸入身份證號碼:?");
String?code?=?scan.nextLine().trim();
scan.close();
String?reg?=?"^(.{6})(.{4})(.{2}).*$";
String?a?=?code.replaceAll(reg,?"$1");
String?y?=?code.replaceAll(reg,?"$2");
String?m?=?code.replaceAll(reg,?"$3");
System.out.println("所在地區代碼:"?+?a);
System.out.println("出生年月:?"?+?y?+?",?"?+?m);
}
}
新聞標題:java出生年月框代碼 java年月日代碼
轉載注明:http://m.newbst.com/article28/docjpjp.html
成都網站建設公司_創新互聯,為您提供企業建站、網站制作、品牌網站制作、定制開發、做網站、網站營銷
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯