仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1063|回复: 18
打印 上一主题 下一主题

[学习教程] JAVA网页编程之格局化输出日期工夫控件 JieFormattedD...

[复制链接]
不帅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:50:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。控件
格局化输出日期工夫控件

能够用JieFormattedDateBoxbox=newJieFormattedDateBox("yyyy年MM月dd日HH点mm分ss秒");输出

也能够用JieFormattedDateBoxbox=newJieFormattedDateBox("HH:mm:ss");来输出工夫

还能够如许用JSpinnerspinner=newJSpinner();JieFormattedDateBoxfdb=newJieFormattedDateBox(format);spinner.setModel(fdb);spinner.setEditor(fdb);



/////////////////////////////////////////////////packageorg.jie.ui.formattedbox;

importjava.sql.Timestamp;importjava.text.SimpleDateFormat;

importjava.awt.event.FocusEvent;importjava.awt.event.FocusListener;importjavax.swing.JFormattedTextField;importjavax.swing.SpinnerModel;importjavax.swing.SwingConstants;importjavax.swing.SwingUtilities;importjavax.swing.event.ChangeListener;importjavax.swing.event.DocumentEvent;importjavax.swing.event.DocumentListener;importjavax.swing.text.DefaultFormatterFactory;importjavax.swing.text.MaskFormatter;

publicclassJieFormattedDateBoxextendsJFormattedTextFieldimplementsSpinnerModel{

privateStringformat;privateSimpleDateFormattimeFormat;privateTimestamptime;

publicvoidsetTime(Timestamptime){if(time!=null)setValue(time);}publicTimestampgetTime(){returntime;}

publicstaticStringreplace(Stringsrc,StringreplaceSrc,StringreplaceWith){if(src==null)returnnull;StringBuffersb=newStringBuffer();intstart=0;intend=src.indexOf(replaceSrc);while(end>=0){sb.append(src.substring(start,end));start=end+replaceSrc.length();end=src.indexOf(replaceSrc,start);sb.append(replaceWith);}sb.append(src.substring(start));returnsb.toString();}

publicJieFormattedDateBox(Stringformat){super();this.format=format;timeFormat=newSimpleDateFormat(format);time=newTimestamp(System.currentTimeMillis());

try{Stringmask=format;for(inti=0;i<alais.length;i++){mask=replace(mask,alais[i],formats[i]);}MaskFormattermf=newMaskFormatter(mask);mf.setPlaceholderCharacter(_);setFormatterFactory(newDefaultFormatterFactory(mf));}catch(Exceptione){e.printStackTrace();}

setText(timeFormat.format(time));this.getDocument().addDocumentListener(newDocumentListener(){publicvoidinsertUpdate(DocumentEvente){checkTime();}publicvoidremoveUpdate(DocumentEvente){checkTime();}publicvoidchangedUpdate(DocumentEvente){}});this.setHorizontalAlignment(SwingConstants.RIGHT);this.addFocusListener(newFocusListener(){publicvoidfocusGained(FocusEvente){}publicvoidfocusLost(FocusEvente){setValue(time);}});}

voidcheckTime(){try{Stringtext=getText();if(text==null||text.equals(""))return;time=newTimestamp(timeFormat.parse(text).getTime());}catch(Exceptionex){try{SwingUtilities.invokeLater(newRunnable(){publicvoidrun(){setValue(time);}});}catch(Exceptione){}}

}



publicObjectgetValue(){returnthis.getText();}publicvoidsetValue(Objectvalue){time=(Timestamp)value;intindex=this.getCaretPosition();if(time==null){this.setText("");}elsethis.setText(timeFormat.format(time));this.setCaretPosition(index);//keepCaretPosition}publicObjectgetNextValue(){returnnewTimestamp(time.getTime()+getIncrease(this.getCaretPosition(),format,time));}publicObjectgetPreviousValue(){returnnewTimestamp(time.getTime()-getDecrease(this.getCaretPosition(),format,time));}publicvoidaddChangeListener(ChangeListenerl){}publicvoidremoveChangeListener(ChangeListenerl){}

staticString[]alais={"yyyy","MM","dd","HH","hh","mm","ss"};staticString[]formats={"####","##","##","##","##","##","##"};staticlong[]times={0,0,86400000,3600000,3600000,60000,1000};publicstaticfinalSimpleDateFormatfullTimeFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");

publicstaticlonggetDecrease(intcaret,Stringformat,Timestamptime){if(testAlias(format,caret,0)){try{StringtimeStr=(Integer.parseInt(newSimpleDateFormat("yyyy").format(time))-1)+"-"+newSimpleDateFormat("MM-ddHH:mm:ss").format(time);returntime.getTime()-fullTimeFormat.parse(timeStr).getTime();}catch(Exceptione){return1000;}}elseif(testAlias(format,caret,1)){try{SimpleDateFormatym=newSimpleDateFormat("yyyy-MM");longthisMonth=ym.parse(ym.format(time)).getTime();longlastMonth=ym.parse(ym.format(newTimestamp(thisMonth-2*times[2]))).getTime();

intdays=(int)((thisMonth-lastMonth)/times[2]);intthisDays=Integer.parseInt(newSimpleDateFormat("dd").format(time));return(thisMonth-lastMonth)+(((thisDays>days)?(thisDays-days):0)*times[2]);}catch(Exceptione){return1000;}}for(inti=2;i<alais.length;i++){if(testAlias(format,caret,i)){returntimes[i];}}return1000;}

publicstaticlonggetIncrease(intcaret,Stringformat,Timestamptime){if(testAlias(format,caret,0)){try{StringtimeStr=(Integer.parseInt(newSimpleDateFormat("yyyy").format(time))+1)+"-"+newSimpleDateFormat("MM-ddHH:mm:ss").format(time);returnfullTimeFormat.parse(timeStr).getTime()-time.getTime();}catch(Exceptione){return1000;}}elseif(testAlias(format,caret,1)){try{SimpleDateFormatym=newSimpleDateFormat("yyyy-MM");longthisMonth=ym.parse(ym.format(time)).getTime();longnextMonth=ym.parse(ym.format(newTimestamp(thisMonth+31*times[2]))).getTime();longnextNextMonth=ym.parse(ym.format(newTimestamp(nextMonth+31*times[2]))).getTime();

intdays=(int)((nextNextMonth-nextMonth)/times[2]);intthisDays=Integer.parseInt(newSimpleDateFormat("dd").format(time));return(nextMonth-thisMonth)-(((thisDays>days)?(thisDays-days):0)*times[2]);}catch(Exceptione){return1000;}}for(inti=2;i<alais.length;i++){if(testAlias(format,caret,i)){returntimes[i];}}return1000;}

privatestaticbooleantestAlias(Stringformat,intcaret,intindex){for(inti=format.indexOf(alais[index]);i>=0;i=format.indexOf(alais[index],i+1)){if(caret>=i&&caret<=i+alais[index].length())returntrue;}returnfalse;}

}

最后我再次声明,我并没有说不看好java,实际上我对java很乐观的,毕竟她正在不断改进中,我相信她总有一天会和.net并驾齐驱的
愤怒的大鸟 该用户已被删除
沙发
发表于 2015-1-21 16:05:02 | 只看该作者
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
透明 该用户已被删除
板凳
发表于 2015-1-30 20:43:47 | 只看该作者
是一种由美国SUN计算机公司(Sun Microsystems, Inc.)所研究而成的语言
飘灵儿 该用户已被删除
地板
发表于 2015-2-4 12:17:54 | 只看该作者
多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。
蒙在股里 该用户已被删除
5#
发表于 2015-2-9 03:51:47 | 只看该作者
象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。
乐观 该用户已被删除
6#
发表于 2015-2-16 06:33:28 | 只看该作者
你现在最缺的是实际的工作经验,而不是书本上那些凭空想出来的程序。
若相依 该用户已被删除
7#
发表于 2015-2-19 11:28:23 | 只看该作者
J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。
山那边是海 该用户已被删除
8#
发表于 2015-2-22 19:52:24 | 只看该作者
Java 不同于一般的编译执行计算机语言和解释执行计算机语言。它首先将源代码编译成二进制字节码(bytecode),然后依赖各种不同平台上的虚拟机来解释执行字节码。从而实现了“一次编译、到处执行”的跨平台特性。
小妖女 该用户已被删除
9#
发表于 2015-3-6 00:32:59 | 只看该作者
如果你学过HTML,那么事情要好办的多,如果没有,那你快去补一补HTML基础吧。其实JSP中的Java语法也不多,它更象一个脚本语言,有点象ASP。
若天明 该用户已被删除
10#
发表于 2015-3-10 04:18:23 | 只看该作者
吧,现在很流行的Structs就是它的一种实现方式,不过Structs用起来实在是很繁,我们只要学习其精髓即可,我们完全可以设计自己的MVC结构。然后你再研究一下软件Refactoring (重构)和极限XP编程,相信你又会上一个台阶。 做完这些,你不如整理一下你的Java代码,把那些经典的程序和常见的应用整理出来,再精心打造一番,提高其重用性和可扩展性。你再找几个志同道合的朋友成立一个工作室吧
冷月葬花魂 该用户已被删除
11#
发表于 2015-3-13 04:10:33 | 只看该作者
是一种使用者不需花费很多时间学习的语言
不帅 该用户已被删除
12#
 楼主| 发表于 2015-4-10 13:43:37 | 只看该作者
多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。
海妖 该用户已被删除
13#
发表于 2015-4-15 10:41:37 | 只看该作者
科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
活着的死人 该用户已被删除
14#
发表于 2015-4-19 05:52:48 | 只看该作者
Java是一个纯的面向对象的程序设计语言,它继承了 C++语言面向对象技术的核心。Java舍弃了C ++语言中容易引起错误的指针(以引用取代)、运算符重载(operator overloading)
15#
发表于 2015-4-21 16:29:40 | 只看该作者
让你能够真正掌握接口或抽象类的应用,从而在原来的Java语言基础上跃进一步,更重要的是,设计模式反复向你强调一个宗旨:要让你的程序尽可能的可重用。
兰色精灵 该用户已被删除
16#
发表于 2015-4-22 17:39:03 | 只看该作者
如果你学过HTML,那么事情要好办的多,如果没有,那你快去补一补HTML基础吧。其实JSP中的Java语法也不多,它更象一个脚本语言,有点象ASP。
分手快乐 该用户已被删除
17#
发表于 2015-5-12 01:31:25 | 只看该作者
是一种使网页(Web Page)产生生动活泼画面的语言
小魔女 该用户已被删除
18#
发表于 2015-6-20 21:41:29 | 只看该作者
Java语言支持Internet应用的开发,在基本的Java应用编程接口中有一个网络应用编程接口(java net),它提供了用于网络应用编程的类库,包括URL、URLConnection、Socket、ServerSocket等。Java的RMI(远程方法激活)机制也是开发分布式应用的重要手段。
admin 该用户已被删除
19#
发表于 2015-6-28 07:11:02 | 只看该作者
《JAVA语言程序设计》或《JAVA从入门到精通》这两本书开始学,等你编程有感觉的时候也可以回看一下。《JAVA读书笔记》这本书,因为讲的代码很多,也很容易看懂,涉及到面也到位。是你学习技术巩固的好书,学完后就看看《JAVA编程思想》这本书,找找一个自己写的代码跟书上的代码有什么不一样。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-5 15:57

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表