我格式化日期字符串时出现错误的日期

时间:2018-10-23 07:04:47

标签: swift date dateformatter

当我尝试格式化日期格式的日期时,我得到了一天,但是输出显示了不同的日期,请任何人为此提供帮助。

    let my_date = "22-03-1995"
    let dateformat = DateFormatter()
    dateformat.dateFormat = "DD-MM-yyyy"
    let date = dateformat.date(from: my_date)
    print(date!)
    text_lbl.text = "\(date!)" 

我的输出是:1995年1月21日是这样。

2 个答案:

答案 0 :(得分:0)

您应该使用

public class Scanner extends Fragment{

public SurfaceView cameraView;
public BarcodeDetector barcode;
public CameraSource cameraSource;
private SurfaceHolder.Callback cameraCallback;
private ActivityScanBinding mbinding;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Log.d("ActivityScan","onCreate");
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    Log.d("ActivityScan","onCreateView");

    mbinding = DataBindingUtil.inflate(inflater, R.layout.activity_scan, container, false);


    mbinding.getRoot().setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }

    });
    cameraView = mbinding.getRoot().findViewById(R.id.cameraView);

    return mbinding.getRoot();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Scan();
}

@Override
public void onDestroy() {

    Log.d("ActivityScan","Destroyed");
    if(barcode!=null) {
        barcode.release();
        Log.d("barcode","Released");
    }
    if(cameraSource!=null) {
        cameraSource.release();
        Log.d("cameraSource ","Released");
    }
    if(cameraView!=null) {
        removeCameraViewCallback();
    }

    super.onDestroy();
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
}



public void Scan(){

    cameraView.setZOrderMediaOverlay(true);

    barcode = new BarcodeDetector.Builder(getActivity())
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();

        if(!barcode.isOperational()){
            return;
        }


    cameraSource = new CameraSource.Builder(getActivity(), barcode)
            .setFacing(CameraSource.CAMERA_FACING_FRONT)
            .setRequestedFps(24)
            .setAutoFocusEnabled(true)
            .setRequestedPreviewSize(1920,1080)
            .build();

    cameraCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

                if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
                    cameraSource.start(cameraView.getHolder());
                }
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.stop();
        }
    };

    cameraView.getHolder().addCallback(cameraCallback);

    barcode.setProcessor(new Detector.Processor<Barcode>() {

        @Override
        public void release() {}

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes =  detections.getDetectedItems();
            if(barcodes.size() > 0){

            }
        }
    });


}

public void removeCameraViewCallback(){
    cameraView.getHolder().removeCallback(cameraCallback);
}
}

这将为您提供let my_date = "22-03-1995" let dateformat = DateFormatter() dateformat.dateFormat = "dd-MM-yyyy" let date = dateformat.date(from: my_date) print(date!)

答案 1 :(得分:-1)

func changeDateFormatFor(date : String,fromFormat : String = "dd-MM-yyyy", toFormat : String = "yyyy-MM-dd")-> String{
if date == "" {
    return ""
}
dateFormatter.dateFormat = fromFormat
let date1 = dateFormatter.date(from: date)
dateFormatter.dateFormat = toFormat
let str = dateFormatter.string(from: date1!)
return str

}