Skip to content

Commit

Permalink
加入Joda-Time,并实现时间公共类,后期根据需要可以在TimeUtil类中继续拓展
Browse files Browse the repository at this point in the history
  • Loading branch information
xwj-vic committed Mar 9, 2017
1 parent 3b253d8 commit 12531cd
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 10 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@
<version>2.3.2</version>
</dependency>

<!--joda-time框架-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>

</dependencies>
<build>
<finalName>ssmshiro</finalName>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package web;
package kcaptcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/service/impl/ShiroUserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

import dao.ShiroUserDao;
import entity.User;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.ShiroUserService;
import util.MD5Util;

import java.util.List;

/**
* Created by xuweijie on 2017/3/6.
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/util/DateTime/TimeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package util.DateTime;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

/**
* Created by xuweijie on 2017/3/8.
* 日期公共类,单例模式(双重检查加锁,可以大大减少getInstance()的同步问题所带来的时间耗费)
*/
public class TimeUtil {

private volatile static TimeUtil timeUtil;
private DateTime dateTime;

public static TimeUtil getInstance(){
if(timeUtil==null){
synchronized (TimeUtil.class){
if(timeUtil==null){
timeUtil=new TimeUtil();
}
}
}
return timeUtil;
}

//自定义表示的某个时刻的时间(具体到时分秒)
public String GetTimeAtPoin(int year,int month,int day,int hour,int min,int sec){
dateTime=new DateTime(year,month,day,hour,min,sec);
return dateTime.toString("yyyy/MM/dd HH:mm:ss EE");
}

public String GetTimeAtPoin(int year,int month,int day){
dateTime=new DateTime(year,month,day,0,0,0);
return dateTime.toString("yyyy/MM/dd EE");
}

//在某个日期上加上xx天(如:在当前日期加上90天)(具体到时分秒)
public String GetTimeAtPoin(int year,int month,int day,int hour,int min,int sec,int additionalTime){
dateTime=new DateTime(year,month,day,hour,min,sec);
return dateTime.plusDays(additionalTime).toString("yyyy/MM/dd HH:mm:ss EE");
}

public String GetTimeAtPoin(int year,int month,int day,int additionalTime){
dateTime=new DateTime(year,month,day,0,0,0);
return dateTime.plusDays(additionalTime).toString("yyyy/MM/dd EE");
}


//获取当前时间
public String CurrentTime(){
return dateTime.toString("yyyy/MM/dd HH:mm:ss EE");
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package util;
package util.Encryption;

import org.apache.shiro.crypto.hash.Md5Hash;

/**
* Created by xuweijie on 2017/3/6.
* MD5+salt对数据进行加密
*/
public class MD5Util {
//shiro提供的MD5加密方法,其中salt是自定义的字符串
Expand Down
1 change: 1 addition & 0 deletions src/main/java/web/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public String login(HttpServletRequest request, Model model){
String verifyCode=request.getParameter("verifyCode").toUpperCase();
String username=request.getParameter("username");
String password=request.getParameter("password");
//判断验证码输入是否正确
if(verifyCode.equals(request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY))){
if((username!=null && password!=null)){
UsernamePasswordToken token=new UsernamePasswordToken(username,password);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/spring/spring-web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@

<!--4:扫描web相关的controller-->
<context:component-scan base-package="web"/>

<context:component-scan base-package="kcaptcha"/>

</beans>
2 changes: 1 addition & 1 deletion src/test/java/testmd5.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import org.junit.Test;
import util.MD5Util;
import util.Encryption.MD5Util;

/**
* Created by xuweijie on 2017/3/7.
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/timetest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.joda.time.DateTime;
import org.junit.Test;
import util.DateTime.TimeUtil;

/**
* Created by xuweijie on 2017/3/8.
*/
public class timetest {

@Test
public void testtime(){

String time=TimeUtil.getInstance().GetTimeAtPoin(2017,3,8,23,33,56);
System.out.println("某一时刻的时间:"+time);
String currenttime=TimeUtil.getInstance().CurrentTime();
System.out.println("当前时间:"+currenttime);
String addtime=TimeUtil.getInstance().GetTimeAtPoin(2017,3,8,23,33,56,90);
System.out.println("某一时刻的时间+加上90天:"+addtime);
String time1=TimeUtil.getInstance().GetTimeAtPoin(2017,3,8);
System.out.println("某一时刻的时间:"+time1);

}
}

0 comments on commit 12531cd

Please sign in to comment.