Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将DateTime拆分为字符串和日期的组合_Java_Api_Csv - Fatal编程技术网

在Java中将DateTime拆分为字符串和日期的组合

在Java中将DateTime拆分为字符串和日期的组合,java,api,csv,Java,Api,Csv,我找到了将以datetime类型返回日期的API。返回的示例是“1994-12-03T12:00:00”,我想将返回修改为“Pca19941203”。返回将应用于csv文件中。我用Java进行修改。有什么方法可以做到这一点吗?Java8+(推荐): import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; 乔达时间: import org.joda.time.LocalDateTime; import

我找到了将以datetime类型返回日期的API。返回的示例是“1994-12-03T12:00:00”,我想将返回修改为“Pca19941203”。返回将应用于csv文件中。我用Java进行修改。有什么方法可以做到这一点吗?

Java8+(推荐)

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
乔达时间:

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
旧Java API:

import java.text.SimpleDateFormat;
输出(来自所有3个)

Pca19941203
您可以执行下一步操作

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class as {

    public static void main(String[] args) {
        Long tmp = castDateToString(new Date());

        System.out.println(tmp);
        //Devolver al servicio
        System.out.println(returnServiceFormat(tmp, "yyyy/MM/dd"));
    }

    public static long castDateToString(Date system) {
        return system.getTime();
    }

    public static String returnServiceFormat(long date, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        DateFormat df = new SimpleDateFormat(format);
        return String.valueOf(df.format(calendar.getTime()));
    }
}
结果是

运行:1579226334699 2020/01/16构建成功(总时间:0秒)


字符串
值转换为所需的
LocalDateTime
对象和格式请不要教年轻人使用过时且臭名昭著的
SimpleDateFormat
类。至少不是第一个选择。而且不是毫无保留的。今天,我们在及其
DateTimeFormatter
中有了更好的功能。您有一个例子来了解什么是正确的吗?如果Andreas的答案是正确的,请参阅Java8+部分。这就是我推荐的做法。另请参阅和的Java8更新部分。谢谢您的回答。它对我有用。我使用Java8+解决方案。
import java.text.SimpleDateFormat;
new SimpleDateFormat("'Pca'yyyyMMdd").format(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                                     .parse("1994-12-03T12:00:00"))
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class as {

    public static void main(String[] args) {
        Long tmp = castDateToString(new Date());

        System.out.println(tmp);
        //Devolver al servicio
        System.out.println(returnServiceFormat(tmp, "yyyy/MM/dd"));
    }

    public static long castDateToString(Date system) {
        return system.getTime();
    }

    public static String returnServiceFormat(long date, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        DateFormat df = new SimpleDateFormat(format);
        return String.valueOf(df.format(calendar.getTime()));
    }
}