使用带有AM / PM格式的TO_DATE()

时间:2016-12-12 21:36:16

标签: sql oracle to-date

我试图从日期格式如下的表格中选择一些日期:

    package nori.beta.musicplayer;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import nori.beta.musicplayer.Class.BlurBuilder;
import nori.beta.musicplayer.Fragment.Playlist;
import nori.beta.musicplayer.Fragment.Utilities;

public class MainActivity extends Activity {

    private ImageView bg;       // blured backgroud of size of screen
    private ImageView cover;    // small image in center of activity that plays song
    private BlurBuilder blured; // class to blur image for background

    private SeekBar
            progressBar;            // Creating seekbar that show progress of song and allow us scroll and rewind song
    private ImageButton
            play_pause_stopButton;  //on click do 1.play/2.paues/3.stop for all change icon
    private MediaPlayer player; // Player that play music
    private Handler mHandler = new Handler(); //Handler that help with refreshing progressBar
    private Utilities utils; //Change seconds into min + sec
    ArrayList<File> mySongs; // list of music file
    ArrayList<Song> songsInfo; //list of music file with extract information about them

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // init all GUI staff
        initGUI();
        setButtons();


    }

    @Override
    protected void onResume() {
        super.onResume();
        // init Database and rest of gui staff that need database
        // Here have u data of all file and chosen song and can u make to play song
    }

    private void initGUI() {
        // Image Part
        bg = (ImageView) findViewById(R.id.main_background);
        cover = (ImageView) findViewById(R.id.cover_image);
        blured = new BlurBuilder();

        //Buttons
        play_pause_stopButton = (ImageButton) findViewById(R.id.play_pause_stop_button);
        progressBar = (SeekBar) findViewById(R.id.progressBar);
        player = new MediaPlayer();
        utils = new Utilities();
        mySongs = findSongs(Environment.getExternalStorageDirectory());
        songsInfo = new ArrayList<Song>();
        for (File f : mySongs) {
            songsInfo.add(new Song(f));
        }
        //progressBar.setOnSeekBarChangeListener(this);

    }

    private ArrayList<File> findSongs(File root) {
        ArrayList<File> al = new ArrayList<File>();
        File[] files = root.listFiles();

        /**
         * findSongs Search for music file in memory
         *
         * for each file in memory
         * 1.if is that file a folder , then  take all file then give it in method findSongs
         * and with requrency
         * 2.Else if that file end with .mp3 or .wav ,then add to list
         */

        for (File singleFile : files) {
            if (singleFile.isDirectory() && !singleFile.isHidden()) {
                al.addAll(findSongs(singleFile));
                //Log.e("findsongs","Folder");
            } else {
                if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
                    al.add(singleFile);
                    Log.e("FileInfo.GetSong", singleFile.getName().toString());
                }
            }
        }
        return al;
    }

    private void setButtons() {
        play_pause_stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //Play/Pause song button clicked
                playSong(0);
            }
        });

        progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { //Using progress bar to scrolling song

            @Override
            public void onStopTrackingTouch(SeekBar progressBar) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar progressBar) {
            }

            @Override
            public void onProgressChanged(SeekBar progressBar, int progress, boolean fromUser) { //When user move progress bar song go to moment that user choosed
                if (player != null && fromUser) {
                    player.seekTo(progress * 1000);
                }
            }
        });
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer player) { //When song ended playing
                playSong(0);
            }
        });
    }

    public void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100); //Updating progressBar every 100ms
    }

    private Runnable mUpdateTimeTask = new Runnable() {
        public void run() { //Updating time of song and progressbar
            long totalDuration = player.getDuration();
            long currentDuration = player.getCurrentPosition();

            // Updating progress bar
            int mCurrentPosition = player.getCurrentPosition() / 1000;
            progressBar.setProgress(mCurrentPosition);

            // Running this thread after 100 milliseconds
            mHandler.postDelayed(this, 100);

        }
    };

    // set it in all changes of the privius songs <#-- Krzysiek -->
    private void setBackground(int i) {
        //setting the back image and cover image to the chosen song
        if (songsInfo.get(i).getBackground() != null) {
            bg.setImageBitmap(blured.blur(this, songsInfo.get(i).getBackground()));
            cover.setImageBitmap(songsInfo.get(i).getBackground());
            Log.i("FileInfo.SetCover", "Set cover of " + songsInfo.get(i).getName());
        }

    }

    public void playSong(int index) {
        try {
            player.reset();
            player.setDataSource(songsInfo.get(index).getPath()); //Getting song with proper index from list
            player.prepare();
            player.start(); //Playing prepared song
            // Displaying Song title
            String songTitle = songsInfo.get(index).getTitle();
            String songArtist = songsInfo.get(index).getArtist();

            setBackground(index);
            // Changing Button Image to pause image
            play_pause_stopButton.setImageResource(R.drawable.pause);

            // set Progress bar values
            progressBar.setProgress(0);
            progressBar.setMax(player.getDuration() / 1000);

            // Updating progress bar
            updateProgressBar();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

请注意,日期可以是AM或PM,但是当我尝试从表中执行简单的SELECT时,例如:

    android:configChanges="orientation|screenSize"

我收到错误:

14-APR-14 10.35.00.0000000000 AM
01-NOV-16 02.43.00.0000000000 PM

我一直试图让这项工作停留一段时间,但没有运气。这里的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

来自你的评论:

  

这实际上是一个时间戳;不是一个字符串。时间戳(6)准确

您可以使用TIMESTAMP literal

SELECT *
FROM   MyTable
WHERE  MyDate > TIMESTAMP '2016-12-31 08:00:00';

答案 1 :(得分:1)

有几个问题。

您的输入显然是字符串,因为它们有10个小数位,Oracle中的时间戳最多只有9个。然后,一小段时间的字符串无法转换为to_date的日期 - 您需要使用to_timestamp,否则您需要删除所有小数部分。在下面的解决方案中,我只删除最后一个(十分之一)小数,因为表中可能有非零小数部分 - 尽管不在您发布的样本中。

然后,你的格式掩码有yyyy,但你的输入只有两位数(这可能意味着93代表1993而不是2093,所以正确使用的是rr而不是yy)。您在输入使用:

的格式掩码中使用.

最后,甚至不要用字符串格式比较日期:在字符串比较中,01-JAN-2015在20-NOV-2013之前。

你可能想要这样的东西:

select mydate
from   (
         select '14-APR-14 10.35.00.0000000000 AM' as mydate from dual
         union all
         select '01-NOV-16 02.43.00.0000000000 PM' from dual
       ) mytable
where to_timestamp(substr(mydate, 1, 28) || substr(mydate, -3), 'dd-MON-rr hh.mi.ss.ff AM')
         > to_timestamp('31-DEC-2016 08:00:00 AM', 'dd-MON-yyyy hh:mi:ss AM');

此查询正确编译,并且在输出中不产生任何行(原因很明显)。

注意:在评论中,您(OP)说mydate字段是timestamp(6)数据类型。很难相信(你显示十位小数),但如果它确实是一个时间戳或日期,那么你不需要将它包装在任何to_timestampto_date函数中,它应该是站立的单独在不平等的左边。