Java时间操作

package com.atirm.mybatismutiplesource;

import com.atirm.mybatismutiplesource.entity.User;
import com.atirm.mybatismutiplesource.service.TestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisMutiplesourceApplicationTests {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private TestService testService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<Object, User> userRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplateLocal;

    @Test
    public void contextLoads() {
        logger.trace("trace内容");
        logger.debug("debug内容");
        logger.info("info内容");
        logger.warn("waring内容");
        logger.error("error内容");
    }

    @Test
    public void getTest() {
        testService.testMultipleSource();
    }

//    @Test
//    public void testRedis() {
//        User user = new User();
//        user.setAge(10);
//        user.setId(1);
//        user.setUsername("alex");
////        redisTemplateLocal.opsForValue().set("user7",user);
//        userRedisTemplate.opsForValue().set("user1000", user);
//    }

    @Test
    public void testData() throws ParseException {
//        Date date = new Date();
//        System.out.println(date);
//        long time = date.getTime();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = simpleDateFormat.format(date);
//        Date parse = simpleDateFormat.parse(format);
//        System.out.println(parse.getTime());
//        GregorianCalendar gregorianCalendar = new GregorianCalendar(2999, 10, 9, 22, 10, 50);
//        System.out.println(gregorianCalendar.get(Calendar.YEAR));
//        System.out.println(gregorianCalendar.get(Calendar.MONTH));
//        System.out.println(gregorianCalendar.get(Calendar.DATE));
        // 设置日期
//        GregorianCalendar calendar3 = new GregorianCalendar(2999, 10, 9, 22, 11, 50);
        // 日期时间对比较
//        calendar3.add(Calendar.YEAR, -1);
//        System.out.println(calendar3.get(Calendar.YEAR));
//        System.out.println("时区" + calendar3.getTimeZone());
//        System.out.println("时间" + calendar3.getTime());
//        System.out.println("时间戳" + calendar3.getTimeInMillis());
        //32467615850000
        //32467615910000
    }

    @Test
    public void testTime() {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.add(Calendar.MINUTE, -15);
        System.out.println(gregorianCalendar.get(Calendar.YEAR));
        System.out.println(gregorianCalendar.get(Calendar.MONTH) + 1);
        System.out.println(gregorianCalendar.get(Calendar.MINUTE));
        // 计算一年有多少周
        System.out.println(gregorianCalendar.getWeeksInWeekYear());
        System.out.println("一周第" + gregorianCalendar.get(Calendar.DAY_OF_WEEK) + "天");
        System.out.println("一月第" + gregorianCalendar.get(Calendar.DAY_OF_MONTH) + "天");
        System.out.println("一月第" + gregorianCalendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) + "周");
        System.out.println("一年第" + gregorianCalendar.get(Calendar.DAY_OF_YEAR) + "天");
        System.out.println(gregorianCalendar.getActualMaximum(Calendar.DATE));
        System.out.println(gregorianCalendar.getActualMaximum(Calendar.YEAR));
    }

    @Test
    public void c() {
        String d1 = "2019-03-06";
        String d2 = "2019-03-07";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            /**
             * 两个时间比较
             */
            Date parse = simpleDateFormat.parse(d1);
            Date parse1 = simpleDateFormat.parse(d2);
            boolean before = parse.before(parse1);
            System.out.println(before);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

 

posted @ 2019-03-06 16:34  雷神约  阅读(141)  评论(0编辑  收藏  举报