찾다
JavaJava베이스Java에서 문자열을 날짜로 변환하는 방법은 무엇입니까?

변환 방법: 1. SimpleDateFormat을 사용하여 시간 형식을 지정합니다. 2. "org.apache.commons.lang3.time.DateUtils"를 사용하여 시간 형식을 지정합니다. 3. DateTimeFormatter를 사용하여 시간 형식을 지정합니다.

Java에서 문자열을 날짜로 변환하는 방법은 무엇입니까?

이 튜토리얼의 운영 환경: windows7 시스템, java8 버전, DELL G3 컴퓨터.

문자열을 날짜로 변환하는 세 가지 일반적인 방법이 있습니다: SimpleDateFormat, org.apache.commons.lang3.time.DateUtils, DateTimeFormatter(Java 8)

Description
SimpleDateFormat 스레드 안전하지 않음, 유연한 텍스트 일치

DateUtils

도구 클래스, 날짜 작업 지원
DateTimeFormatter

스레드 안전성, LocalDateTime을 사용한 체인 프로그래밍 지원, 비교 작업 용이

다음은 샘플 코드

/**
     *     指定当前时间-指定时间是否大于30秒
     */
    
    //SimpleDateFormat
    private static void m1() throws ParseException {
        String endTime = "哈哈2020-02-07 18:58:02.0你好";//支持特殊格式转换
        String format = "哈哈yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date edate = sdf.parse(endTime);
        Date now = new Date();
        String nowStr = DateFormatUtils.format(now, format);
        if(DateUtils.addSeconds(edate, 30).before((now))){
            logger.info("true endTime={} now={}",endTime, nowStr);
        }else{
            logger.info("false endTime={} now={}",endTime, nowStr);
        }
    }

    //DateUtils
    private static void m3() throws ParseException {
        String endTime = "2020-02-07 18:58:02.0";
        String format = "yyyy-MM-dd HH:mm:ss";
        Date edate = DateUtils.parseDate(endTime, format, "yyyy-MM-dd HH:mm:ss.SSS");//支持多格式匹配
        Date now = new Date();
        String nowStr = DateFormatUtils.format(now, format);
        if(DateUtils.addSeconds(edate, 30).before((now))){
            logger.info("true endTime={} now={}",endTime, nowStr);
        }else{
            logger.info("false endTime={} now={}",endTime, nowStr);
        }
    }

    //DateTimeFormatter
    private static void m2(){
        String endTime = "2020-02-07 18:58:02";
        String format = "yyyy-MM-dd HH:mm:ss";
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        String nowStr = now.format(dateTimeFormatter);
        if(LocalDateTime.parse(endTime, dateTimeFormatter)
                .plusSeconds(30).isBefore(LocalDateTime.now())){//链式编程
            logger.info("true endTime={} now={}",endTime, nowStr);
        }else{
            logger.info("false endTime={} now={}",endTime, nowStr);
        }
    }

추천 관련 비디오 튜토리얼: Java 비디오 튜토리얼

위 내용은 Java에서 문자열을 날짜로 변환하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구